Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a WPF application that has user controls being loaded on button clicks. As a report each page is a user control and I load multiple user controls as multiple pages.
When I try to print using FixedDocument if I call PrintDialog.ShowDialog for each user control I am able to print the controls correctly, but that means the user has to setup the printer for each page.
Is there a way I can show the PrintDialog only once, get the printer settings and then apply the settings for the rest of the pages and print the pages subsequently?

What I have tried:

I have tried to share the FixedDocument object across the calls to the print function I have that calls through Document.Pages.Add().
However as mentioned it works if I have the ShowDialog for each iteration and fails otherwise.
Posted
Updated 7-Jan-18 8:12am

1 solution

Yes, there's a way! You have to call PrintDialog on the beginning of the process, then you have to loop through the "WPF pages".

For example:
C#
// select printer and get printer settings
PrintDialog pd = new PrintDialog();
//catch when user cancel process
//if (!pd.ShowDialog()) return;

// create a document and set PageSize from PrintDialog!
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

//create page by page
foreach(var p in YourPages) //loop through your collection of controls  
{
    // create a page
    FixedPage page1 = new FixedPage();
    page1.Width = document.DocumentPaginator.PageSize.Width; //trick!
    page1.Height = document.DocumentPaginator.PageSize.Height; //trick!
    //further instructions
}


See:
WPF Printing Part 2 – The Fixed Document[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900