温馨提示×

C# 打印输出怎么设置纸张大小

c#
小樊
200
2024-07-20 06:16:42
栏目: 编程语言

在C#中打印输出时,可以使用PrintDocument类来设置纸张大小。以下是一个示例代码,演示如何设置纸张大小:

using System;
using System.Drawing;
using System.Drawing.Printing;

class Program
{
    static void Main()
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintPage);
        
        // 设置纸张大小
        pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 300, 400);
        
        // 打印输出
        pd.Print();
    }

    private static void PrintPage(object sender, PrintPageEventArgs e)
    {
        // 在打印页面上绘制内容
        Graphics g = e.Graphics;
        g.DrawString("Hello, world!", new Font("Arial", 12), Brushes.Black, new PointF(100, 100));
    }
}

在上面的示例中,我们创建了一个PrintDocument对象,并设置了纸张大小为300x400。然后通过PrintPage事件处理程序来绘制内容,在这里我们简单地绘制了一段文字。最后调用Print()方法来执行打印输出。

请注意,具体支持的纸张大小取决于打印机驱动程序和设置,可能会因打印机的硬件和驱动程序而有所不同。

0