WPF: Printing a FlowDocument without a Print Dialog

I am writing a note taking application in WPF using FlowDocument for each individual note. The application searches and filters notes by tags. I want to print all the notes in the current filtered list as separate documents, and I want to show only one print dialog at the beginning of the job.

I found a good print example in this thread , but it is designed to print a single FlowDocument , so it uses the CreateXpsDocumentWriter() overload, which displays the Print Dialog. A.

So here is my question: can anyone suggest some good code to print a FlowDocument without displaying a PrintDialog ? I believe that I will show the Print Dialog at the beginning of the procedure, and then scroll through the collection of notes to print each FlowDocument .

+6
printing wpf flowdocument
source share
2 answers

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.

+3
source share

Just a loop after you get printDialog .

 for(int i=0 i<document.count i++) printdocument((document[i] as iDocumentPaginator),"title"+[i]); 
+1
source share

All Articles