Rob
Rob

Reputation: 26324

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:

Upvotes: 7

Views: 3842

Answers (1)

themechanix1
themechanix1

Reputation: 195

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

Upvotes: 13

Related Questions