Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:21:57+00:00 2026-05-13T20:21:57+00:00

Various WPF applications of mine display FlowDocument’s. I’m able to print them, using the

  • 0

Various WPF applications of mine display FlowDocument’s. I’m able to print them, using the approach described in the answer to Printing a WPF FlowDocument.

Now I’d like to add a “print preview” capability. In the normal case, I am printing the FlowDocument that is displayed in the Window, and so I wouldn’t need a Print Preview then. But in some cases the FlowDocument to print is constructed on-the-fly in memory. And in these cases I’d like to display it before printing.

Now, I can certainly pop a new window and display the FlowDocument, but

  1. I want the preview to really feel like it is part of the printing operation, and not just another Window in the app.

  2. I don’t want a normal FlowDocument in a FlowDocumentScrollViewer. Rather than being “any size” it needs to be constrained to the size of the paper, a specific HxW ratio, and paginated.

Suggestions?

  • should I just use a standard Window, and in that case, how to I ensure the FlowDocument is at the proper ratio?

  • is there a more “integrated” way to do the preview within the scope of the PrintDialog UI that is part of Windows?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:21:57+00:00Added an answer on May 13, 2026 at 8:21 pm

    Taking the hint from the comment added to my question, I did this:

    private string _previewWindowXaml =
        @"<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'>
            <DocumentViewer Name='dv1'/>
         </Window>";
    
    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("'", "&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
                {
                }
            }
        }
    } 
    

    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.

    I also added some code to hide the Search box. See this answer to WPF: How can I remove the searchbox in a DocumentViewer? for how I did that.

    The effect is like this:

    alt text

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seen various examples of WPF applications I've seen the use of the Grid control
I'm benchmarking a WPF application on various platforms and I need an easy way
One thing i noticed about WPF is that it combines various concepts from other
For various reasons calling System.exit is frowned upon when writing Java Applications , so
For various silly reasons, I'd like to be able to detect the rectangle of
What is currently the best solution for WPF applications which are meant to be
Various programs can do stuff only when you haven't used the computer for a
For various reasons, such as cookies, SEO, and to keep things simple, I would
For various reasons, we are writing a new business objects/data storage library. One of
On various pages throughout my PHP web site and in various nested directories I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.