打印文档时的页面范围问题

发布于 2024-12-05 07:49:04 字数 1056 浏览 1 评论 0原文

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

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”)并单击打印。在 printdocument() 上方,我设置了一个断点并查看了 printdialog-object。它显示 pd.PageRangeSelection = PageRangeSelection.UserPagepd.PageRange = {1-3}。我想这是对的,因为我只想打印第 1-3 页。然后执行 printdocument() 并在输出 pdf 中(为了测试,我使用 pdf 打印机)有 14 页(整个文档已打印)。

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

感谢您的帮助

i try to print out the content of my editor:

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

The test-document i used has about 14 pages (with this pagesize-settings).
i tested it: the printdialog appears and I´ve chosen a pagerange (i typed "1-3" into the textbox) and clicked print. above the printdocument() I set a breakpoint and looked into the printdialog-object. it says pd.PageRangeSelection = PageRangeSelection.UserPage and pd.PageRange = {1-3}. I guess this is right, because I wanted to print out only page 1-3. then the printdocument() executed and in the output-pdf (for testing I use a pdf-printer) has 14 pages (the whole document was printed).

where is my mistake? why does the pagerange-setting not work?

thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-12-12 07:49:04

在您的代码中手动设置:

pd.PageRangeSelection = PageRangeSelection.AllPages;

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

In your code you manually set:

pd.PageRangeSelection = PageRangeSelection.AllPages;

This is why your code prints all the pages.

嘦怹 2024-12-12 07:49:04

原因是 FlowDocumentDocumentPaginator 不处理 UserPageRange。您可以看到 FlowDocument 实现创建一个 FlowDocumentPaginator,以及它不考虑范围。

如果它确实处​​理了它,请在 FlowDocumentPaginator 中。 (Async)GetPage 您会看到,代码检查以查看请求打印的页面是否在可用页面的索引中;或者,如果 Dictionary 中存在一个键,其值为要打印的 DocumentPage

换句话说,PrintDialog 默认将 UserPageRangeEnabled 设置为 false 的原因是,为了使用该功能,您通常需要必须编写自己的 DocumentPaginator 或者必须添加一些逻辑来编译新的临时文档以仅保存要打印的页面。

如有任何问题,请随时提出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文