Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF printing FlowDocuments to XPS: printed content not stretching across page. Why?

I'm trying to print a FlowDocument with PrintDialog to an XPS file. The resulting printed content only appears to stretch across half the XPS page, not the entire width of the page. Here is an example of what the resulting XPS document looks like in Windows XPS viewer:

Example of XPS document in Windows XPS viewer(note: it looks exactly the same if I print it with a printer on normal 8x11 printing paper)

This is the code I'm using to print this document:

void Print()
{
    PrintDialog printDialog = new PrintDialog();
    bool? result = printDialog.ShowDialog();
    if (!result.HasValue)
        return;
    if (!result.Value)
        return;

    double pageWidth = printDialog.PrintableAreaWidth;
    double pageHeight = printDialog.PrintableAreaHeight;
    FlowDocument flowDocument = CreateFlowDocument(pageWidth, pageHeight);

    printDialog.PrintDocument(
        ((IDocumentPaginatorSource)flowDocument).DocumentPaginator,
        "Test print job");
}

FlowDocument CreateFlowDocument(double pageWidth, double pageHeight)
{
    FlowDocument flowDocument = new FlowDocument();
    flowDocument.PageWidth = pageWidth;
    flowDocument.PageHeight = pageHeight;
    flowDocument.PagePadding = new Thickness(30.0, 50.0, 20.0, 30.0);
    flowDocument.IsOptimalParagraphEnabled = true;
    flowDocument.IsHyphenationEnabled = true;
    flowDocument.IsColumnWidthFlexible = true;

    Paragraph header = new Paragraph();
    header.FontSize = 18;
    header.Foreground = new SolidColorBrush(Colors.Black);
    header.FontWeight = FontWeights.Bold;
    header.Inlines.Add(new Run("Title of my document (will be cut off in XPS)";));
    flowDocument.Blocks.Add(header);

    Paragraph test = new Paragraph();
    test.FontSize = 12;
    test.Foreground = new SolidColorBrush(Colors.Black);
    test.FontWeight = FontWeights.Bold;
    test.Inlines.Add(new Run("This text should stretch across the entire width of the page. Let's see if it really does, though."));
    flowDocument.Blocks.Add(test);

    return flowDocument;
}

pageWidth is 816.0 and pageHeight is 1056.0, which should be more than big enough to accommodate my text. What could be going wrong?


Edit: Here are some other things I've tried:

  • Tried adding a StackPanel with a large width to my Paragraph in the FlowDocument. Text just gets cut off about halfway across the page.
  • Tried assigning FlowDocument.PageWidth to a large width. Again, the text just gets cut off about halfway across the page.
like image 315
Rob Avatar asked Feb 19 '11 17:02

Rob


1 Answers

Try setting the ColumnWidth property of FlowDocument to pageWidth - that should fix it.

like image 69
themechanix1 Avatar answered Oct 19 '22 17:10

themechanix1