Search code examples
canvasprintingsizeprintdocument

The page size cannot be modified when printing with PrintDocument in C# on certain machines


It is a very simple program. Literally print "Hello World" with the selected canvas size. It works fine on 4 machines in my test (including Win 7 and Win 8), but doesn't work on 2 other machines (both Windows 7), on which even I selected ANSI E, the printed result is still ANSI A.

some notes here:

  1. I printed with PDFLite, Adobe Acrobat, and to Microsoft XPS, the results are the same.
  2. I can successfully print a notepad text file on Ansi E canvas through pdflite and other pdf writer. Some other tools can print successfully to ANSI E through PdfLite as well.

The program basically:

  1. select the page size to ANSI E
  2. Click the print button, pops up the select printer window.
  3. Select a virtual printer, such as PDFLite, click print.
  4. Generate PDF, which is still ANSI A.

Here is the complete source (without UI code):

 private void PrintButton_Click(object sender, RoutedEventArgs e)
    {
        PrintDocument printDocument = new PrintDocument();
        printDocument.DefaultPageSettings.PaperSize = GetPaperSize();
        printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

        System.Windows.Forms.PrintDialog printWindowDialog = new System.Windows.Forms.PrintDialog();
        printWindowDialog.Document = printDocument;
        if (printWindowDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            printDocument.Print();
        }
    }

    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        ev.Graphics.DrawString("Hello World", new Font("Arial", 10), Brushes.Black, 5, 0, new StringFormat());
    }

    private PaperSize GetPaperSize()
    {
        PaperSize printPreviewPaperSize = new PaperSize("AnsiA", 850, 1100);
        switch (((ComboBoxItem)sizeCb.SelectedItem).Content.ToString())
        {
            case "ANSI A – 8.5'' x 11''":
            default:
                printPreviewPaperSize = new PaperSize("AnsiA", 850, 1100);
                break;
            case "ANSI B – 11'' x 17''":
                printPreviewPaperSize = new PaperSize("AnsiB", 1100, 1700);
                break;
            case "ANSI C – 17'' x 22''":
                printPreviewPaperSize = new PaperSize("AnsiC", 1700, 2200);
                break;
            case "ANSI D – 22'' x 34''":
                printPreviewPaperSize = new PaperSize("AnsiD", 2200, 3400);
                break;
            case "ANSI E – 34'' x 44''":
                printPreviewPaperSize = new PaperSize("AnsiE", 3400, 4400);
                break;
        }

        return printPreviewPaperSize;
    }

So is there something wrong in the code? Please advise. Thanks for the help!

Ben


Solution

  • That's because the default settings in the dialog replaced your manual page size. Try this and it works fine.

    if (printWindowDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         printDocument.PrinterSettings.DefaultPageSettings.PaperSize = = new PaperSize("AnsiE", 3400, 4400);
         printDocument.DefaultPageSettings.PaperSize = new PaperSize("AnsiE", 3400, 4400);
         printDocument.Print();
    }