none
FlowDocument text disappears after printing. RRS feed

  • Question

  • My program has a RichTextBox with a FlowDocument nested inside.  For example:

    <RichTextBox>
       <FlowDocument Name="fd">
          <Paragraph>I am trying to print this...</Paragraph>
       </FlowDocument>
    </RichTextBox>


    I want to print the content of the FlowDocument.  Following the sample code from the book: PRO WPF: Windows Presentation Foundation in .NET, page 668:

    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == true)
    {
    printDialog.PrintDocument(
    ((IDocumentPaginatorSource)fd).DocumentPaginator,
    "A Flow Document");
    }


    The content DOES print, but after printing, the on-screen display of the text disappears.  To get anything back, i have to restart the application.

    How can I get the RichTextBox text to stay visible after printing?

    Thanks for the help!
    John
    Wednesday, January 16, 2008 9:23 PM

Answers

  • Not clear why this is needed, but as suggested from Prajakta's code sample, a copy should be made against FlowDocument before printing it out:

    Code Block
    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == true)
    {
    MemoryStream stream = new MemoryStream();
    TextRange sourceDocument = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    sourceDocument.Save(stream, DataFormats.Xaml);

    FlowDocument flowDocumentCopy = new FlowDocument();
    TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd);
    copyDocumentRange.Load(stream, DataFormats.Xaml);

    printDialog.PrintDocument(
    ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator,
    "Printing Richtextbox Content");
    }

    Tuesday, January 22, 2008 4:21 AM

All replies

  • Thanks, but i couldn't find anything in that article that helps my problem.

    John
    Friday, January 18, 2008 2:25 AM
  • Could you please show us how you print the document? in particular how do you construct DocumentPaginator instance?

    Thanks
    Monday, January 21, 2008 9:22 AM
  • I do exactly as described in the PRO WPF book, which is also really the same as this msdn page:
    http://msdn2.microsoft.com/en-us/library/aa970917.aspx

    Filtering out all the non-important stuff, it all comes down to this:


    <RichTextBox Name="rtb">
       <FlowDocument>
          <Paragraph>I am trying to print this...</Paragraph>
       </FlowDocument>
    </RichTextBox>


    Add a button to activate printing.  Add this to the event handler:

    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == true)
    {
    printDialog.PrintDocument(
    ((IDocumentPaginatorSource)rtb.Document).DocumentPaginator,
    "Printing Richtextbox Content");
    }


    The paragraph does print (although it's placed badly and cut off).  But that can be fixed.  The problem here is that the text in the RichTextBox disappears from the UI after printing.

    This example is as simple as it gets.  If i can get this to work, then i should be able to fix my actual real world app.

    Doesn't this happen to anyone else?

    Thanks for the continued help.
    John
    Monday, January 21, 2008 7:28 PM
  • Not clear why this is needed, but as suggested from Prajakta's code sample, a copy should be made against FlowDocument before printing it out:

    Code Block
    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == true)
    {
    MemoryStream stream = new MemoryStream();
    TextRange sourceDocument = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    sourceDocument.Save(stream, DataFormats.Xaml);

    FlowDocument flowDocumentCopy = new FlowDocument();
    TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd);
    copyDocumentRange.Load(stream, DataFormats.Xaml);

    printDialog.PrintDocument(
    ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator,
    "Printing Richtextbox Content");
    }

    Tuesday, January 22, 2008 4:21 AM
  • Marco,

    Thanks, that solves the problem!  So the code prints from a copy, thus not messing up the text in the UI.  Nice fix.  Thanks!! Smile

    John
    Wednesday, January 30, 2008 5:29 PM