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.
Reader Level:

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.

Login to add your contents and source code to this article
post comment
     

this is very interesting, but how can we make a custom page size, e.g 4 x 6

Posted by mukhlim fazal May 10, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter