Page Range Problem When Printing a Document

I am trying to print the contents 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 contains about 14 pages (with these page settings). I tested it: printdialog appears, and I selected pagerange (I typed "1-3" into the text box) and hit print . above printdocument() I set a breakpoint and looked at the printdialog object. he says pd.PageRangeSelection = PageRangeSelection.UserPage and pd.PageRange = {1-3} . I think this is correct because I only wanted to print page 1-3. then the executed printdocument() 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 the help

+6
source share
2 answers

The reason for this is that the FlowDocument DocumentPaginator does not process UserPageRange s. You can see that the FlowDocument implementation creates the FlowDocumentPaginator , and it does not take ranges into account.

If he could handle this, in the FlowDocumentPaginator.(Async)GetPage you will see that the code checks if the requested page has been printed and the index of available pages has been printed; or maybe if the key exists in the Dictionary whose value is the DocumentPage to print. A.

In other words, and the reason the UserPageRangeEnabled set to false by the default UserPageRangeEnabled is because you usually have to write your own DocumentPaginator to use this function, or you need to add some logic to compile a new temporary document to store only pages, which you want to print.

Feel free to ask any questions.

+1
source

In your code that you manually installed:

 pd.PageRangeSelection = PageRangeSelection.AllPages; 

This is why your code prints all pages.

0
source

Source: https://habr.com/ru/post/897570/


All Articles