Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking the hint from the comment added to my question, I did this: </p> <pre><code>private string _previewWindowXaml = @"&lt;Window xmlns ='http://schemas.microsoft.com/netfx/2007/xaml/presentation' xmlns:x ='http://schemas.microsoft.com/winfx/2006/xaml' Title ='Print Preview - @@TITLE' Height ='200' Width ='300' WindowStartupLocation ='CenterOwner'&gt; &lt;DocumentViewer Name='dv1'/&gt; &lt;/Window&gt;"; internal void DoPreview(string title) { string fileName = System.IO.Path.GetRandomFileName(); FlowDocumentScrollViewer visual = (FlowDocumentScrollViewer)(_parent.FindName("fdsv1")); try { // write the XPS document using (XpsDocument doc = new XpsDocument(fileName, FileAccess.ReadWrite)) { XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc); writer.Write(visual); } // Read the XPS document into a dynamically generated // preview Window using (XpsDocument doc = new XpsDocument(fileName, FileAccess.Read)) { FixedDocumentSequence fds = doc.GetFixedDocumentSequence(); string s = _previewWindowXaml; s = s.Replace("@@TITLE", title.Replace("'", "&amp;apos;")); using (var reader = new System.Xml.XmlTextReader(new StringReader(s))) { Window preview = System.Windows.Markup.XamlReader.Load(reader) as Window; DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(preview, "dv1") as DocumentViewer; dv1.Document = fds as IDocumentPaginatorSource; preview.ShowDialog(); } } } finally { if (File.Exists(fileName)) { try { File.Delete(fileName); } catch { } } } } </code></pre> <p>What it does: it actually prints the content of a visual into an XPS document. Then it loads the "printed" XPS document and displays it in a very simple XAML file that is stored as a string, rather than as a separate module, and loaded dynamically at runtime. The resulting Window has the DocumentViewer buttons: print, adjust-to-max-page-width, and so on. </p> <p>I also added some code to hide the Search box. See <a href="https://stackoverflow.com/questions/2322727/wpf-how-can-i-remove-the-searchbox-in-a-documentviewer/2323275#2323275">this answer to <em>WPF: How can I remove the searchbox in a DocumentViewer?</em></a> for how I did that.</p> <p>The effect is like this: </p> <p><a href="http://i48.tinypic.com/2hzkfat.jpg" rel="nofollow noreferrer">alt text http://i48.tinypic.com/2hzkfat.jpg</a></p> <p>The XpsDocument can be found in the ReachFramework dll and the XpsDocumentWriter can be found in the System.Printing dll both of which must be added as references to the project</p>
 

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