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:
The program basically:
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
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();
}