---
title: "How to print a WPF FlowDocument?"  
description: "How to print a WPF FlowDocument?"  
author: "Samuel Fernandes"  
published: 2013-08-16  
updated: 2013-08-17  
canonical: https://www.mindstick.com/forum/1384/how-to-print-a-wpf-flowdocument  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# How to print a WPF FlowDocument?

Hi!

I'm [building](https://www.mindstick.com/blog/23088/which-tonewood-is-suitable-for-building-a-guitar) a demo app in WPF, which is new to me. I'm currently displaying text in a FlowDocument, and need to [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) it.

The [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) I'm using looks like this:

PrintDialog pd = new PrintDialog();

fd.PageHeight = pd.PrintableAreaHeight;

fd.PageWidth = pd.PrintableAreaWidth;

fd.PagePadding = new Thickness(50);

fd.ColumnGap = 0;

fd.ColumnWidth = pd.PrintableAreaWidth;

IDocumentPaginatorSource dps = fd;

pd.PrintDocument(dps.DocumentPaginator, "flow doc");

fd is my FlowDocument, and for now I'm using the [default](https://www.mindstick.com/interview/12771/what-is-the-importance-of-default-resources) printer instead of allowing the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to specify print options. It works OK, [except](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) that after the [document](https://www.mindstick.com/articles/328642/what-to-consider-while-choosing-a-software-documentation-tool) prints, the FlowDocument displayed on [screen](https://www.mindstick.com/forum/33644/loading-screen-during-ajax-call) has changed to to use the [settings](https://www.mindstick.com/blog/63563/steps-to-change-the-wireless-settings-using-linksys-cloud-account) I specified for printing.

I can fix this by manually resetting everything after I print, but is this the best way? Should I make a copy of the FlowDocument before I print it? Or is there another [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) I should consider?

Thanks.

## Replies

### Reply by shreesh chandra shukla

Hi!

yes, make a copy of the FlowDocument before printing it. This is because the pagination and margins will be different. This works for me.

```
    private void DoThePrint(System.Windows.Documents.FlowDocument document)    {        // Clone the source document's content into a new FlowDocument.        // This is because the pagination for the printer needs to be        // done differently than the pagination for the displayed page.        // We print the copy, rather that the original FlowDocument.        System.IO.MemoryStream s = new System.IO.MemoryStream();        TextRange source = new TextRange(document.ContentStart, document.ContentEnd);        source.Save(s, DataFormats.Xaml);        FlowDocument copy = new FlowDocument();        TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);        dest.Load(s, DataFormats.Xaml);        // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,        // and allowing the user to select a printer.        // get information about the dimensions of the seleted printer+media.        System.Printing.PrintDocumentImageableArea ia = null;        System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);        if (docWriter != null && ia != null)        {            DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;            // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.            paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);            Thickness t = new Thickness(72);  // copy.PagePadding;            copy.PagePadding = new Thickness(                             Math.Max(ia.OriginWidth, t.Left),                               Math.Max(ia.OriginHeight, t.Top),                               Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),                               Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));            copy.ColumnWidth = double.PositiveInfinity;            //copy.PageWidth = 528; // allow the page to be the natural with of the output device            // Send content to the printer.            docWriter.Write(paginator);        }    }
```

thanks


---

Original Source: https://www.mindstick.com/forum/1384/how-to-print-a-wpf-flowdocument

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
