【问题标题】:WPF printing FlowDocuments to XPS: printed content not stretching across page. Why?WPF 将 FlowDocuments 打印到 XPS:打印的内容不会跨页延伸。为什么?
【发布时间】:2011-06-30 10:57:03
【问题描述】:

我正在尝试将带有PrintDialogFlowDocument 打印到XPS 文件中。生成的打印内容似乎只延伸到 XPS 页面的 一半,而不是整个页面的宽度。以下是在 Windows XPS 查看器中生成的 XPS 文档的示例:

(注意:如果我用打印机在普通的 8x11 打印纸上打印它看起来完全一样)

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

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;
}

pageWidth816.0pageHeight1056.0,应该更多 足以容纳我的文字。可能出了什么问题?


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

  • 尝试在FlowDocument 中向我的Paragraph 添加一个宽大的StackPanel。文本在页面的一半左右被截断。
  • 尝试将FlowDocument.PageWidth 分配给较大的宽度。同样,文本在页面的一半左右被截断。

【问题讨论】:

    标签: wpf printing .net-3.5


    【解决方案1】:

    尝试将FlowDocumentColumnWidth 属性设置为pageWidth - 这应该可以解决问题。

    【讨论】:

    • 在您回答之前我已经浪费了大约 20 页。谢谢)
    猜你喜欢
    • 2012-01-18
    • 2012-12-05
    • 2011-09-27
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多