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开发者_StackOverflow社区 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
In your code you manually set:
pd.PageRangeSelection = PageRangeSelection.AllPages;
This is why your code prints all the pages.
The reason for this is because FlowDocument
's DocumentPaginator
does not handle UserPageRange
s. 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.
精彩评论