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:
- Set up which printer your document is using first by either hard coding
'printDocument1.PrinterSettings.PrinterName' or using the Print Dialog.
- Call the method from above, for example:
ForcePageSize(this.printDocument1,System.Drawing.Printing.PaperKind.A4);
-
Call 'printDocument1.Print()'as normal.