这是一个非常简单的程序。字面打印" Hello World"使用选定的画布大小。它在我的测试中的4台机器上运行良好(包括Win 7和Win 8),但不能在其他2台机器(Windows 7)上工作,即使我选择ANSI E,打印结果仍然是ANSI A 。
这里有一些注意事项:
该计划基本上是:
这是完整的源代码(没有UI代码):
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;
}
代码中有什么问题吗?请指教。谢谢你的帮助!
本
答案 0 :(得分:0)
那是因为对话框中的默认设置取代了您的手册页面大小。试试这个,它工作正常。
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();
}