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:
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)
retu开发者_运维问答rn;
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 myParagraph
in theFlowDocument
. 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.
Try setting the ColumnWidth
property of FlowDocument
to pageWidth
- that should fix it.
精彩评论