我已經重寫了我對這個問題的回答,因爲我找到了更好的方法來打印一組FlowDocuments,同時只顯示一次Print對話框。答案來自MacDonald,Pro WPF在C#2008(Apress 2008)第20章中的p。 704.
我的代碼將一組Note對象綁定到一個名爲notesToPrint的IList中,並從我的應用程序中DocumentServices類的每個Note獲取FlowDocument。它將FlowDocument邊界設置爲與打印機匹配並設置1英寸的邊距。然後使用文檔的DocumentPaginator屬性打印FlowDocument。這裏的代碼:
// Show Print Dialog
var printDialog = new PrintDialog();
var userCanceled = (printDialog.ShowDialog() == false);
if(userCanceled) return;
// Print Notes
foreach(var note in notesToPrint)
{
// Get next FlowDocument
var collectionFolderPath = DataStore.CollectionFolderPath;
var noteDocument = DocumentServices.GetFlowDocument(note, collectionFolderPath) ;
// Set the FlowDocument boundaries to match the page
noteDocument.PageHeight = printDialog.PrintableAreaHeight;
noteDocument.PageWidth = printDialog.PrintableAreaWidth;
// Set margin to 1 inch
noteDocument.PagePadding = new Thickness(96);
// Get the FlowDocument's DocumentPaginator
var paginatorSource = (IDocumentPaginatorSource)noteDocument;
var paginator = paginatorSource.DocumentPaginator;
// Print the Document
printDialog.PrintDocument(paginator, "FS NoteMaster Document");
}
這是一個非常簡單的方法,有一個顯着的限制:它不會異步打印。要做到這一點,你必須在後臺線程上執行這個操作,這就是我的做法。
我仍然想找到一個更好的方法來做到這一點。如果有人可以提出建議,我會改變接受的答案。 – 2010-08-30 22:38:17
您可以嘗試使用PrintDialog.PrintQueue和PrintDialog.PrintTicket成員。使用PrintQueue您可以創建XpsDocumentWriter,然後您可以使用WriteAsync()異步打印。 緩存隊列和票證似乎比緩存PrintDialog更好。 – 2010-10-06 19:21:54
謝謝 - 這很有幫助。從我+1。 – 2010-10-07 19:06:42