Sunday, September 24, 2017

Changing PrinterSettings.Copies Not Working

Leave a Comment

I am using WPF .Net Framework 4.0

What I expect is 3 pages, but I am only getting 1

var doc = new System.Drawing.Printing.PrintDocument(); document.PrintPage += Document_PrintPage; document.Print();  private void Document_PrintPage(object sender, PrintPageEventArgs e) {     e.PageSettings.PrinterSettings.Copies = 3;     e.PageSettings.PrinterSettings.DefaultPageSettings.PrinterSettings.Copies = 3;     ... } 

It works when I set PrinterSettings.Copies before document.Print()

document.PrinterSettings.Copies = 3; 

But this way it prints all pages 3 times

But e.HasMorePages can be true and each page can have different copy count, so I have to set it inside Document_PrintPage

1 Answers

Answers 1

This behavior is very much by design. The MSDN docs could be a bit more explicit about it, and an exception wouldn't hurt, but this restriction is very much a side-effect of the printing architecture in Windows. The printer driver needs to know this detail up front. Your PrinterSettings are used at the start of the print job and cannot be changed while printing is in progress. What you can do while printing is limited, but otherwise well represented by the QueryPageSettings event. Which allows changes to the PageSettings only.

In a WPF app you'd more likely use the System.Printing namespace, the number of copies is set by the PrintTicket.CopyCount. Exact same limitation, you can't change the ticket while printing.

The only other thing you can do is render the page repeatedly to get the desired number of copies. I'll have to wing it, the snippet in the question is not detailed enough to determine how you set the HasMorePages property. We'll need some variables:

    public bool IsPrintPreview { get; set; }   // True to avoid copies while previewing     public int NumberOfPages { get; set; }     // Determines HasMorePages      private int PageNumber;                    // Page number while printing     private int PageCopyCount;                 // Copy counter 

And I'll make up a method that determines how many copies should be rendered for each individual page:

    private int GetNumberOfCopies(int page) {         if (page > NumberOfPages) return 0;         if (IsPrintPreview) return 1;         // TODO: modify this         return 3;     } 

We need the BeginPrint event to initialize the counters:

    private void Document_BeginPrint(object sender, PrintEventArgs e) {         PageNumber = 1;         PageCopyCount = GetNumberOfCopies(PageNumber);         if (PageCopyCount == 0) e.Cancel = true;     } 

And the logic in the PrintPage event to count the copies and pages:

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {         // Draw output for page# PageNumber         //...         // Count copies and pages         e.HasMorePages = true;         if (--PageCopyCount == 0) {             PageNumber += 1;             PageCopyCount = GetNumberOfCopies(PageNumber);             if (PageCopyCount == 0) e.HasMorePages = false;         }     } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment