Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So I got it working after reading <a href="http://rads.stackoverflow.com/amzn/click/1590599551" rel="noreferrer">Pro WPF in C# 2008</a> (Page 726).</p> <p>Basically the DocumentViewer class needs an XPS file to present a print preview of it. So I do the following:</p> <pre><code>PrintDialog dialog = new PrintDialog(); var paginator = new RowPaginator(rowsToPrint) { PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight) }; string tempFileName = System.IO.Path.GetTempFileName(); //GetTempFileName creates a file, the XpsDocument throws an exception if the file already //exists, so delete it. Possible race condition if someone else calls GetTempFileName File.Delete(tempFileName); using (XpsDocument xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite)) { XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument); writer.Write(paginator); PrintPreview previewWindow = new PrintPreview { Owner = this, Document = xpsDocument.GetFixedDocumentSequence() }; previewWindow.ShowDialog(); } </code></pre> <p>I'm creating the print dialog to get the default page size. There's probably a better way to do this. XpsDocument is in ReachFramework.dll (Namespace System.Windows.Xps.Packaging);</p> <p>Here's the PrintPreview Window.</p> <pre><code>&lt;Window x:Class="WPFPrintTest.PrintPreview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="previewWindow" Title="PrintPreview" Height="800" Width="800"&gt; &lt;Grid&gt; &lt;DocumentViewer Name="viewer" Document="{Binding ElementName=previewWindow, Path=Document}" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>The code behind just has a Document property like so:</p> <pre><code>public IDocumentPaginatorSource Document { get { return viewer.Document; } set { viewer.Document = value; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload