Reader Level:
ARTICLE

Hard Coding Pape Size during Printing

Posted by samuel.ludlow Articles | Printing in C# July 14, 2003
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.
  • 0
  • 0
  • 23516

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:

bool ForcePageSize(System.Drawing.Printing.PrintDocument MyPrintDocument, System.Drawing.Printing.PaperKind MyPaperKind)
{
for (int i = 0; i < MyPrintDocument.PrinterSettings.PaperSizes.Count; ++i)
{
if
(MyPrintDocument.PrinterSettings.PaperSizes[i].Kind == MyPaperKind)
{
MyPrintDocument.DefaultPageSettings.PaperSize = MyPrintDocument.PrinterSettings.PaperSizes[i];
return true;
}
}
return false;
}

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.

COMMENT USING

Trending up