WPF FlowDocument只打印到小区域

5

我正在使用FlowDocument、FlowDocumentPaginator和PrintDialog在WPF中打印纯文本。我的方法基于this article,实现如下:

        var printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {
            var flowDocument = new FlowDocument();

            var paragraph = new Paragraph();
            paragraph.FontFamily = new FontFamily("Courier New");
            paragraph.FontSize = 10;
            paragraph.Margin = new Thickness(0);
            paragraph.Inlines.Add(new Run(this.textToPrint));

            flowDocument.FontSize = 10;
            flowDocument.Blocks.Add(paragraph);

            var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
            printDialog.PrintDocument(paginator, "Chit");
        }

这适用于打印窄宽内容的情况。但是当我尝试打印一个长字符串时,所有内容都塞在了一个小区域里:

enter image description here

我在打印对话框的 PrintTicket 和分页器中检查了尺寸,它们似乎都没问题:

enter image description here

那么,是什么导致了这个问题,我该如何解决?

我现在不在工作岗位上,但我可以告诉你它正在创建两列。如果你制作一个长文档,你就会看到它。你需要告诉FlowDocument它是单列,并告诉FlowDocument打印机的宽度。 - paparazzo
观察得很好。请问您可以在方便的时候,进一步阐述如何在回答中实现您的建议吗?[编辑:我看到您已经这样做了。谢谢。] - Gigi
1个回答

10

这是我使用的一些代码

flowDocument.PagePadding = new Thickness(standardThickness);
flowDocument.ColumnGap = 0;
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
你需要告诉 FlowDocument 它只有一列,并告诉它打印机的宽度。

太好了,这个可行。我建议你在回答中添加你在注释中写的内容(两列的东西),这样人们就可以在其中找到所有相关的信息。 - Gigi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接