5

我有一个 Windows 窗体应用程序,带有一个包含面板的简单 Windows 窗体。我已将面板大小设置为:560、579(以像素为单位),并以这种方式设置打印文档大小:

System.Drawing.Printing.PaperSize a = new System.Drawing.Printing.PaperSize("A5 (148 x 210 mm)", 584, 827);
printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = a;

现在我希望 printpreviewdialoge 页面大小为 a5 或至少与我的打印文档大小相同并适合它,我该如何实现?

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Bitmap b = new Bitmap(pnlPrint.Width, pnlPrint.Height);
        pnlPrint.DrawToBitmap(b, new System.Drawing.Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
        e.Graphics.DrawImage(b,0,0);
    }

    private void Print()
    {
    PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
    var paperSize = printDocument1.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == "A5");
    printDocument1.PrinterSettings.DefaultPageSettings.PaperSize = paperSize; 
    printPreviewDialog1.Document = printDocument1;

    printPreviewDialog1.ShowDialog();

    }
4

2 回答 2

8

PaperSize您可以从对象的PrinterSettings属性中获取 A5PrintDocument对象。它具有PaperSizes保存所选打印机的所有纸张尺寸的属性。您可以使用 LINQ 找到您想要的。例如:

var paperSize = printDoc.PrinterSettings.PaperSizes.Cast<PaperSize>().FirstOrDefault(e => e.PaperName == "A5");
printDoc.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
于 2012-09-10T12:59:07.117 回答
1

只需从以下链接查看我的答案:

从 C# 打印信封

谢谢你。

于 2015-02-05T14:37:56.033 回答