I rewrote my answer to this question because I found the best way to print the FlowDocuments set, showing the print dialog only once. Response received from MacDonald, Pro WPF in C # 2008 (Apress 2008) in chapter 20 on page 704.
My code binds a set of Note objects in an IList called notesToPrint and gets a FlowDocument for each note from the DocumentServices class in my application. It sets the boundaries of the FlowDocument to match the printer and sets a 1-inch marker. He then prints the FlowDocument using the DocumentPaginator property of the document. Here is the code:
// 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 DocumentPaginator var paginatorSource = (IDocumentPaginatorSource)noteDocument; var paginator = paginatorSource.DocumentPaginator; // Print the Document printDialog.PrintDocument(paginator, "FS NoteMaster Document"); }
This is a fairly simple approach with one significant limitation: it does not print asynchronously. To do this, you will need to perform this operation in the background thread, as I do it.
David veeneman
source share