WPF Printing Part 4 – Print Preview

This is the 4th post in a series about printing in WPF, you may want to take a look at the previous posts about printing WPF visuals, using FixedDocument objects and WPF pixel sizes.

There are many ways to print in WPF, in this series I’m constructing FixedDocument objects and then print them, at first it may look like there are easier or more powerful ways to print – until you need a to the print preview feature.

WPF has a DocumentViewer control that can display a FixedDocument on the screen with paging, panning, zooming and a print button – exactly everything we need in a print preview window, this makes writing the print preview feature ridiculously easy.

First the XAML (that can't get much simpler than that):

<Window x:Class="Printing1.PrintPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Print Preview">
    <DocumentViewer Name="_viewer"/>
</Window>

Next the c# (that also doesn't contain much):

using System;
using System.Windows;
using System.Windows.Documents;

namespace Printing1
{
    public partial class PrintPreview : Window
    {
        public PrintPreview()
        {
            InitializeComponent();
        }

        public IDocumentPaginatorSource Document
        {
            get { return _viewer.Document; }
            set { _viewer.Document = value; }
        }
    }
}

And last but no least, replace the printing code from out previous application with:

PrintPreview preview = new PrintPreview();
preview.Owner = this;
preview.Document = document;
preview.ShowDialog();

It’s that simple.

We even get a search box – that doesn’t work! to remove the search box you need to modify the DocumentViewer’s control template, if anyone knows how to make that search box work please leave a comment or use the contact form.

posted @ Thursday, July 9, 2009 2:45 PM

Comments on this entry:

# re: WPF Printing Part 4 – Print Preview

Left by Helen at 9/23/2009 6:17 PM

This is to preview a document. What about previewing visuals? What you did at Part 1 but with previewing. Thankyou

# re: WPF Printing Part 4 – Print Preview

Left by Phil at 7/28/2014 12:18 PM

Many thanks for the informative article. I got as far as you did and hit the same problem. The trick to getting this working is to convert the FixedDocument to an XpsDocument.

First of all, add references to ReachFramework and System.Printing so that you can get to the XPS classes. Then add code to do the transform... e.g.

/// <summary>
/// Helper method to construct an
/// XPS document from a vanilla
/// FixedDocument
/// </summary>
/// <param name="fixedDoc">The fixed document</param>
/// <returns>An XPS document</returns>
private XpsDocument GetXpsDocument(FixedDocument fixedDoc)
{
MemoryStream ms = new MemoryStream();
Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
string pack = "pack://temp.xps";
PackageStore.AddPackage(new Uri(pack), pkg);

XpsDocument xpsDoc = new XpsDocument(pkg, CompressionOption.SuperFast, pack);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
writer.Write(fixedDoc);

return xpsDoc;
}

You can then change your source where you set preview.Document to refer to:
GetXpsDocument(document).GetFixedDocumentSequence();

This allows text selection and searching from within the DocumentViewer window.

Your comment:



 (will not be displayed)


 
 
Please add 6 and 3 and type the answer here: