How to combine multiple PrintVisual() in 1 print-job?

Viewed 932

I have a C# WPF application (.NET 4.6) that needs to print a number of Page objects.
Each Page is setup in the XAML editor to be exactly 1 page of A4 in size and the content is build from a number of label, border and image components placed on the Page.
I added a method to my Page class that adds some dynamic data at run-time to the Page and then prints it using PrintVisual().
In fact: Its a loop that fills a Page object with the dynamic content and then calls PrintVisual() to print it. For each iteration, some new content is placed in the Page. (See sample code.)

// Print method of the Page object.
public void PrintIt(int totalpages)
{
     PrintDialog pDialog = new PrintDialog();
     // If not cancelled then print 
     if (pDialog.ShowDialog() == false) return;  // User cancelled. Don't bother
     for(int pagenum=1; i<=totalpages; i++)
     {
         // Omitted several dozen statements that fill/update the content of various 
         // label components based on the pagenumber.
         this.UpdateLayout();     // Required to re-render new page properly before printing.
         pDialog.PrintVisual(this, "My PrintJob");
     }
}

This works well, expect for the fact that each PrintVisual() creates a separate print-job, which makes it impossible to print double-sided (and when printing to PDF each page ends up in a new file).

I need to tie all pages (PrintVisual() calls) in a single print-job. I'm fairly sure that I need to use a PrintDocument(), but I can't seem to figure out how to construct the required FlowDocument and populate it with my actual prints.

I'm quite new to WPF programming so I'm probably missing something obvious, but as far as I can't tell there isn't any simple way to do this.
Can anyone give me a push in the right direction ?

0 Answers
Related