WPF打印FlowDocuments到XPS:打印内容不跨页面延伸。为什么?

时间:2011-02-19 17:32:15

标签: wpf printing .net-3.5

我正在尝试将带有FlowDocument的{​​{1}}打印到XPS文件中。生成的打印内容仅显示在XPS页面的一半上,而不是页面的整个宽度。以下是Windows XPS查看器中生成的XPS文档的示例:

Example of XPS document in Windows XPS viewer (注意:如果我用普通8x11打印纸上的打印机打印它看起来完全相同)

这是我用来打印此文档的代码:

PrintDialog

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; } 816.0 pageWidth 1056.0 ,应该更多而不是足以容纳我的文本。可能出现什么问题?


编辑:以下是我尝试过的其他一些事情:

  • 尝试向pageHeight中的StackPanel添加宽度较大的Paragraph。文字只是在整个页面的中途被切断了。
  • 尝试将FlowDocument分配给较大的宽度。同样,文本在页面的中间位置被切断了。

1 个答案:

答案 0 :(得分:13)

尝试将ColumnWidth的{​​{1}}属性设置为FlowDocument - 应该修复它。