我正在使用C#的PrintDocument类通过虚拟postscript打印机和ghostscript创建pdf。我希望能够在PrintPage事件中更改页面的大小,即在打印页面时。我知道可以在打印文档之前以及QueryPageSettings事件中更改页面大小。
这是我演示问题的示例代码:
PrintDocument doc = new PrintDocument();
doc.PrinterSettings.PrinterName = "Printer Name";
// V This can change the page size V
doc.DefaultPageSettings.PaperSize = new PaperSize("Custom", 1000, 200);
doc.QueryPageSettings += (sender, args) =>
{
// V This can also change the page size V
args.PageSettings.PaperSize = new PaperSize("Custom", 1000, 200);
};
doc.PrintPage += (sender, args) =>
{
// V This can not change the page size V
doc.PrinterSettings.PaperSize = new PaperSize("Custom", 1000, 200);
// V This changes the paper size for future pages V
args.PageSettings.PaperSize = new PaperSize("Custom", 1000, 200);
args.Graphics.DrawString("Text", new Font("Courier New", 13.0f), Brushes.Black, 0f, 50);
};
doc.Print();