Click here to Skip to main content
15,886,801 members
Articles / Desktop Programming / WPF

WPF Visual Print Component

Rate me:
Please Sign up or sign in to vote.
4.80/5 (14 votes)
11 Aug 2011CPOL5 min read 93.9K   7.1K   33  
A look at how to print a visual element with the WPF Visual Print
#region File Info
// File       : PreviewWindow.xaml.cs
// Description: 
// Package    : VisualPrint
//
// Authors    : Fred Song
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;
using System.IO;
using System.IO.Packaging;
using System.Windows.Markup;
using System.Xml;
using System.Windows.Xps.Serialization;

namespace VisualPrint
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class PreviewWindow : Window
    {
        public PreviewWindow(FlowDocument fdoc)
        {
            InitializeComponent();
            Viewer.Document = fdoc;
            Viewer.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
        }

        #region RoutedEvents

        public static readonly RoutedEvent PrintEvent = EventManager.RegisterRoutedEvent(
            "Print", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PreviewWindow));

        public event RoutedEventHandler Print
        {
            add { AddHandler(PrintEvent, value); }
            remove { RemoveHandler(PrintEvent, value); }
        }

        public FlowDocument Document { get { return Viewer.Document; } }

        #endregion

        private void OnPrintClick(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(PrintEvent));
            Close();
        }

        private void OnClosed(object sender, EventArgs e)
        {
            Viewer.Document = null;
            //make sure the GC collects everything
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
Fred is a senior software developer who lives in Melbourne, Australia. In 1993, he started Programming using Visual C++, Visual Basic, Java, and Oracle Developer Tools. From 2003, He started with .Net using C#, and then expertise .Net development.

Fred is often working with software projects in different business domains based on different Microsoft Technologies like SQL-Server, C#, VC++, ASP.NET, ASP.Net MVC, WCF,WPF, Silverlight, .Net Core and Angular, although he also did some development works on IBM AS400.

Comments and Discussions