c#printdocument默认打印纸大小设置

据我所知c#printdocument默认打印为A4纸。可以设置默认打印为A5纸吗?怎样设置?

引用chatgpt部分指引作答:
在C#中,PrintDocument类默认使用系统的默认打印纸张大小,通常是A4纸。但你可以通过设置PrinterSettings对象来更改默认的打印纸张大小。下面是一种设置默认打印纸张大小为A5的方法:

using System.Drawing.Printing;

// 创建一个PrintDocument对象
PrintDocument printDocument = new PrintDocument();

// 创建一个PrinterSettings对象并关联PrintDocument对象的PrinterSettings属性
PrinterSettings printerSettings = new PrinterSettings();
printDocument.PrinterSettings = printerSettings;

// 设置打印纸张大小为A5
printerSettings.DefaultPageSettings.PaperSize = new PaperSize("A5", 583, 827); // 设置宽度和高度(以百分之一英寸为单位)

// 打印文档
printDocument.Print();

在上面的示例中,我们通过创建PrinterSettings对象并将其关联到PrintDocument对象的PrinterSettings属性,然后将打印纸张大小设置为A5,最后通过调用printDocument.Print()方法来打印文档。

请注意,打印机本身必须支持A5纸张大小才能正常打印。

该回答引用GPT与博主@晓码自在合作编写:

是的,C#中的PrintDocument默认打印尺寸是A4。可以通过以下步骤设置默认打印尺寸为A5:

  1. 在PrintPage事件处理方法中获取PrintDocument对象的DefaultPageSettings属性:
csharp
private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    System.Drawing.Printing.PageSettings settings = printDocument1.DefaultPageSettings;
} 

  1. 修改DefaultPageSettings的PaperSize属性为PaperKind.A5:
csharp
settings.PaperSize = System.Drawing.Printing.PaperKind.A5;

  1. 设置打印机可用的PaperSizes,确保其中包含A5尺寸。如果不设置,程序在运行时会抛出异常。
csharp 
printDocument1.PrinterSettings.PaperSizes = new System.Drawing.Printing.PaperSize[] 
    { 
        System.Drawing.Printing.PaperKind.A4,
        System.Drawing.Printing.PaperKind.A5 
    };

  1. 在PrintDocument的BeginPrint事件中设置PrinterSettings的DefaultPageSettings为修改后的PageSettings。这将使PrintDocument的默认打印尺寸变为A5。
csharp
private void PrintDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    printDocument1.PrinterSettings.DefaultPageSettings = settings; 
}

综上,完整代码如下:

csharp
public partial class Form1 : Form 
{
    private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Printing.PageSettings settings = printDocument1.DefaultPageSettings;
        settings.PaperSize = System.Drawing.Printing.PaperKind.A5;
    }

    private void PrintDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
    {
        printDocument1.PrinterSettings.PaperSizes = new System.Drawing.Printing.PaperSize[] 
        { 
            System.Drawing.Printing.PaperKind.A4,
            System.Drawing.Printing.PaperKind.A5 
        };
        printDocument1.PrinterSettings.DefaultPageSettings = settings; 
    }
}

希望以上解释和示例代码能帮助你设置C# PrintDocument的默认打印尺寸为A5。