Page-Range-Problem at Printing a Document
Asked Answered
C

2

6

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

Cramoisy answered 18/9, 2011 at 12:33 Comment(0)
S
1

In your code you manually set:

pd.PageRangeSelection = PageRangeSelection.AllPages;

This is why your code prints all the pages.

Superfine answered 19/9, 2011 at 8:37 Comment(1)
this is not the reason. i already said that i have debugged it and the PageRangeSelection-Property is set to UserPages before the PrintDocument()-Method is executedCramoisy
M
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.

Morbilli answered 26/12, 2014 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.