2011-09-18 49 views
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頁(此頁面大小的設置)。 我測試了它:printdialog出現了,我選擇了頁面範圍(我在文本框中輸入了「1-3」),然後點擊print。在printdocument()之上我設置了一個斷點並查看了printdialog對象。它說pd.PageRangeSelection = PageRangeSelection.UserPagepd.PageRange = {1-3}。我想這是對的,因爲我只想打印出1-3頁。然後printdocument()執行並在輸出pdf(用於測試我使用pdf打印機)有14頁(整個文件被打印)。

我的錯誤在哪裏?爲什麼頁面範圍設置不起作用?

感謝您的幫助

回答

0

在你的代碼手動設置:

pd.PageRangeSelection = PageRangeSelection.AllPages; 

這就是爲什麼你的代碼打印的所有網頁。

+0

這不是原因。我已經說過我已經調試過它並且在執行PrintDocument()方法之前將PageRangeSelection-Property設置爲UserPages – 0xDEADBEEF

1

原因是因爲FlowDocumentDocumentPaginator不處理UserPageRange s。您可以看到FlowDocument實現創建FlowDocumentPaginator,並沒有考慮範圍。

如果確實處理了它,在FlowDocumentPaginator.(Async)GetPage中,您會看到代碼檢查以查看請求打印的頁面是否位於可用頁面的索引中;或者如果中存在密鑰,其值是要打印的DocumentPage

換句話說,和PrintDialog默認已UserPageRangeEnabled設置爲false的原因,是因爲爲了使用該功能,你通常需要編寫自己的DocumentPaginator,或者你必須添加一些邏輯編譯新臨時文檔僅保存您想要打印的頁面。

隨時問任何問題。