Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / WPF

SharpVectors - SVG# Reloaded: An Introduction

Rate me:
Please Sign up or sign in to vote.
4.98/5 (33 votes)
17 Nov 2010BSD10 min read 204.4K   21.7K   101  
A C# library for converting SVG to WPF and viewing SVG files in WPF Applications
using System;
using System.IO;
using System.IO.Packaging;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;

namespace WpfTestSvgSample
{
    /// <summary>
    /// Interaction logic for PrintPreviewWindow.xaml
    /// </summary>
    public partial class PrintPreviewWindow : Window
    {
        #region Private Fields

        private string      _fileName;
        private Package     _xpsDocPackage;
        private XpsDocument _xpsDocument;

        #endregion

        #region Constructors and Destructor

        public PrintPreviewWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OnWindowLoaded);
            this.Unloaded += new RoutedEventHandler(OnWindowUnloaded);

            this.Closed += new EventHandler(OnWindowClosed);
            this.Closing += new CancelEventHandler(OnWindowClosing);
        }

        #endregion

        #region Public Methods

        public void LoadDocument(XpsDocument document, Package package, string sourceFileName)
        {
            if (document == null)
            {
                return;
            }

            try
            {
                if (_xpsDocument != null)
                {
                    _xpsDocument.Close();
                    _xpsDocument = null;
                }

                if (_xpsDocPackage != null)
                {
                    _xpsDocPackage.Close();
                    _xpsDocPackage = null;
                }

                if (!String.IsNullOrEmpty(_fileName))
                {
                    PackageStore.RemovePackage(new Uri(_fileName));
                }
            }
            catch
            {
            }

            _xpsDocument   = document;
            _xpsDocPackage = package;
            _fileName      = sourceFileName;

            docViewer.Document = _xpsDocument.GetFixedDocumentSequence();
        }

        #endregion

        #region Private Methods

        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            ContentControl findToolbar = docViewer.Template.FindName(
                "PART_FindToolBarHost", docViewer) as ContentControl;
            if (findToolbar != null)
            {
                findToolbar.Visibility = Visibility.Collapsed;
            }
        }

        private void OnWindowUnloaded(object sender, RoutedEventArgs e)
        {
            try
            {
                docViewer.Document = null;

                if (_xpsDocument != null)
                {
                    _xpsDocument.Close();
                    _xpsDocument = null;
                }

                if (_xpsDocPackage != null)
                {
                    _xpsDocPackage.Close();
                    _xpsDocPackage = null;
                }

                if (!String.IsNullOrEmpty(_fileName))
                {
                    PackageStore.RemovePackage(new Uri(_fileName));
                }
            }
            catch
            {
            }
        }

        private void OnWindowClosing(object sender, CancelEventArgs e)
        {
            try
            {
                docViewer.Document = null;

                if (_xpsDocument != null)
                {
                    _xpsDocument.Close();
                    _xpsDocument = null;
                }

                if (_xpsDocPackage != null)
                {
                    _xpsDocPackage.Close();
                    _xpsDocPackage = null;
                }

                if (!String.IsNullOrEmpty(_fileName))
                {
                    PackageStore.RemovePackage(new Uri(_fileName));
                }
            }
            catch
            {
            }
        }

        private void OnWindowClosed(object sender, EventArgs e)
        {
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Japan Japan
Systems Engineer

Comments and Discussions