6

我尝试打印出我的编辑器的内容:

PrintDialog pd = new PrintDialog();

pd.PageRangeSelection = PageRangeSelection.AllPages;
pd.UserPageRangeEnabled = true;

FlowDocument fd = DocumentPrinter.CreateFlowDocumentForEditor(CurrentDocument.Editor);
DocumentPaginator dp = ((IDocumentPaginatorSource)fd).DocumentPaginator;

bool? res = pd.ShowDialog();

if (res.HasValue && res.Value)
{
    fd.PageHeight = pd.PrintableAreaHeight;
    fd.PageWidth = pd.PrintableAreaWidth;
    fd.PagePadding = new Thickness(50);
    fd.ColumnGap = 0;
    fd.ColumnWidth = pd.PrintableAreaWidth;

    pd.PrintDocument(dp, CurrentDocument.Editor.FileName);
}

我使用的测试文档大约有 14 页(使用此页面大小设置)。我测试了它:出现打印对话框,我选择了一个页面范围(我在文本框中输入了“1-3”)并点击了print。在上面printdocument()我设置了一个断点并查看了 printdialog-object。它说pd.PageRangeSelection = PageRangeSelection.UserPagepd.PageRange = {1-3}。我想这是对的,因为我只想打印第 1-3 页。然后printdocument()执行并在输出pdf(用于测试我使用pdf打印机)有14页(打印了整个文档)。

我的错误在哪里?为什么页面范围设置不起作用?

感谢您的帮助

4

2 回答 2

1

The reason for this is because FlowDocument's DocumentPaginator does not handle UserPageRanges. You can see that FlowDocument implementation creates a FlowDocumentPaginator, and it doesn't take into account ranges.

If it did handle it, in FlowDocumentPaginator.(Async)GetPage you would see, code checking to see if the page requested to be printed is in an index of available pages; or maybe if a key exists in a Dictionary whose value is the DocumentPage to print.

In other words, and the reason the PrintDialog default has UserPageRangeEnabled set to false, is because in order to use that feature, you'll usually have to write your own DocumentPaginator or you have to add some logic to compile a new temporary document to hold only the pages you want to print.

Feel free to ask any questions.

于 2014-12-26T22:20:22.920 回答
1

在您的代码中,您手动设置:

pd.PageRangeSelection = PageRangeSelection.AllPages;

这就是您的代码打印所有页面的原因。

于 2011-09-19T08:37:26.717 回答