WPF FlowDocument仅打印到小区域

时间:2016-07-05 21:47:17

标签: c# wpf printing flowdocument

我使用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

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

1 个答案:

答案 0 :(得分:6)

这是我使用的一些代码

flowDocument.PagePadding = new Thickness(standardThickness);
flowDocument.ColumnGap = 0;
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;

您需要告诉flowdocument它是一列并告诉flowdocument打印机的宽度。