WPF: Customizing WPF PrintDialog in Framework 4.8 to Add Input Box for Page Selection

Jane Jie Chen 506 Reputation points
2025-03-18T13:52:04.6433333+00:00

Our WPF application targets .NET 4.8.

The WPF application can generate run report which may include 12 pages.

A run report allows print. So far there is no option, and it always prints all 12 pages.

We like to add feature to allow select page numbers and only print those selected pages.

we notice that WPF PrintDialog allows set two options:

UserPageRangeEnabled and SelectedPagesEnabled

PrintOptions

if users select "Pages" option and provide valid page range, click the Print button and Users will get PageFrom and PageTo values in PrintDialog.PageRange object.

However, if users select "Selection" option and there is no Input box in the Print dialog for users to provide selected page numbers.

Reference the link:

https://learn.microsoft.com/en-us/answers/questions/2225264/wpf-printdialog-how-to-enable-and-detect-selected?page=1&orderby=Helpful&translated=false#answers

After selecting "Selection" option and click the Print button, users need to provide another Input Box and provided selected page numbers.

Just have a feeling that WPF PrintDialog in framework 4.8 does not provide full solution. Both Selection and Input box for the Selection option should be included in the PrintDialog.

Is there a possible to customize the WPF PrintDialog in Framework 4.8 to add the Input box for "Selection" option? thx!

PrintDialog printDialog = new PrintDialog();      
printDialog.UserPageRangeEnabled = true; 
printDialog.SelectedPagesEnabled = true;

 if ( printDialog.ShowDialog() == false )
    return;

 DocumentPaginator paginator = DocumentSource.DocumentPaginator;
 if (printDialog.PageRangeSelection == PageRangeSelection.SelectedPages)
 {
     SelectedDiaglog selectedDiaglog = new SelectedDiaglog();
     if (selectedDiaglog.ShowDialog() == true) // If the user clicks Confirm
     {
        string input = selectedDiaglog.UserInput; // Get the string input by the user
        var selectNum = input.Split(',').ToList();
        //TODO: Use the page number to find the corresponding data for printing
     }
 }
 else if (printDialog.PageRangeSelection == PageRangeSelection.UserPages)
 {
     paginator = new PageRangeDocumentPaginator(
                    paginator, printDialog.PageRange);
     
     {
         printDialog.PrintVisual(paginator.GetPage(pageNumber).Visual, "PrintReport");
     }
 }
Developer technologies Windows Presentation Foundation
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.