Hard Coding Pape Size during Printing

Every time I set up a new Windows printer I spend a few minutes going through every bit of the printer properties making sure that every bit is set to A4. The code discussed in this article shows, how you can do it programmatically.

If I get it wrong then the printer will start asking to put Letter in or the margins will be wrong.

Here is a method that will force your paper size without your users spending 10 minutes in the Page and Printer Dialogues:

  1. bool ForcePageSize(System.Drawing.Printing.PrintDocument MyPrintDocument, System.Drawing.Printing.PaperKind MyPaperKind) {  
  2.  for (int i = 0; i < MyPrintDocument.PrinterSettings.PaperSizes.Count; ++i) {  
  3.   if (MyPrintDocument.PrinterSettings.PaperSizes[i].Kind == MyPaperKind) {  
  4.    MyPrintDocument.DefaultPageSettings.PaperSize = MyPrintDocument.PrinterSettings.PaperSizes[i];  
  5.    return true;  
  6.   }  
  7.  }  
  8.  return false;  
  9. }
Steps:

 

  1. Set up which printer your document is using first by either hard coding

    'printDocument1.PrinterSettings.PrinterName' or using the Print Dialog.

  2. Call the method from above, for example:

    ForcePageSize(this.printDocument1,System.Drawing.Printing.PaperKind.A4);

  3. Call 'printDocument1.Print()'as normal.


Similar Articles