IRONSOFTWAREHOME

如何在 C# 中设置打印纸张尺寸

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

IronPrint的PaperSize属性为开发人员提供了对纸张尺寸的直接控制。 我们从PrintSettings对象传递给任何IronPrint的打印方法。 打印机随后会使用该确切纸张尺寸来处理该任务。

本指南将通过可运行的 C# 代码,逐步演示如何设置标准纸张尺寸、将纸张尺寸与其他打印设置结合使用,以及如何进行异步打印。

快速入门:设置纸张尺寸
  1. 通过NuGet安装IronPrint: Install-Package IronPrint
  2. 添加using IronPrint;到文件
  3. 创建一个PrintSettings对象
  4. PaperSize.A4)
  5. 将设置传递给Printer.PrintAsync()
  1. 1Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2复制并运行这段代码。

    using IronPrint;
    
    // Print a PDF on A4 paper
    Printer.Print("report.pdf", new PrintSettings
    {
        PaperSize = PaperSize.A4
    });
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPrint
    arrow pointer

如何在 C# 中设置打印纸张尺寸?

我们通过将PaperSize枚举中的值分配给PrintSettings对象上的PaperSize属性来设置纸张尺寸。 然后我们将该对象传递给 IronPrint 的任何打印方法

using IronPrint;

// Configure print settings with US Letter paper
PrintSettings settings = new PrintSettings();
settings.PaperSize = PaperSize.Letter;

// Print to the default printer
Printer.Print("invoice.pdf", settings);

首先,我们实例化PrintSettings,它使用打印机的默认值初始化。 然后,我们用PaperSize,这映射到标准的8.5 × 11英寸格式。 当我们调用Printer.Print时,IronPrint使用该纸张尺寸将文档发送到系统的默认打印机。

如果没有指定PaperSize.PrinterDefault,这将推迟到操作系统默认打印机配置要使用的纸张尺寸。 对于打印机配置因机器而异的生产环境而言,这是至关重要的细节。

该库支持哪些纸张尺寸?

PaperSize枚举包括十二个值,涵盖ISO国际标准、常见的美国尺寸和打印机默认值。 下表列出了所有可用的选项。

枚举值标准尺寸(毫米)尺寸(英寸)
PaperSize.A0ISO A0841 × 118933.1 × 46.8
PaperSize.A1ISO A1594 × 84123.4 × 33.1
PaperSize.A2ISO A2420 × 59416.5 × 23.4
PaperSize.A3ISO A3297 × 42011.7 × 16.5
PaperSize.A4ISO A4210 × 2978.3 × 11.7
PaperSize.A5ISO A5148 × 2105.8 × 8.3
PaperSize.B4ISO B4250 × 3539.8 × 13.9
PaperSize.B5ISO B5176 × 2506.9 × 9.8
PaperSize.Letter美国信函216 × 2798.5 × 11.0
PaperSize.Legal美国法律216 × 3568.5 × 14.0
PaperSize.Executive美国高管184 × 2677.25 × 10.5
PaperSize.PrinterDefault打印机默认设置不同不同

每个数值均直接对应于公认的纸张标准。 PrinterDefault选项告诉IronPrint使用打印机当前配置的任何尺寸——这在尊重最终用户打印机偏好而不是强制特定格式时非常有用。

如需完整的 API 参考,请参阅 PaperSize 类文档

如何将纸张尺寸与其他打印设置结合使用?

PaperSize之外的属性。 我们可以配置页面方向DPI页边距、复印份数和灰度模式——所有设置均可在一个对象中完成。

using IronPrint;

// Configure full print settings
PrintSettings settings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Landscape,
    Dpi = 300,
    NumberOfCopies = 3,
    PaperMargins = new Margins(15, 15, 15, 15),
    Grayscale = false
};

// Print the quarterly report
Printer.Print("quarterly-report.pdf", settings);

此处为清晰起见,采用了对象初始化器语法。 Orientation将A4纸张旋转到其宽的一侧。 DPI在300时确保图表和细文本的清晰输出。 Margins构造函数接受四个以毫米为单位的值——上、右、下、左。 1,但我们明确设置以便于阅读。

这些特性能够协同工作且互不冲突。 IronPrint 会验证配置,并将合并后的设置作为单个打印作业传递给打印机驱动程序。 有关打印机选择纸盒配置等高级设置,请参阅完整的打印设置指南

如何使用自定义纸张尺寸进行异步打印?

对于不可以阻塞主线程的应用程序——如WPF或WinForms应用程序——我们使用Printer.PrintAsync。 该方法接受相同的Task

using IronPrint;
using System.Threading.Tasks;

public class DocumentPrinter
{
    public async Task PrintLegalDocumentAsync(string filePath)
    {
        // Configure Legal paper size
        PrintSettings settings = new PrintSettings
        {
            PaperSize = PaperSize.Legal,
            PaperOrientation = PaperOrientation.Portrait,
            Dpi = 300
        };

        // Print asynchronously
        await Printer.PrintAsync(filePath, settings);
    }
}

这个基于类的示例展示了一个现实的模式,其中DocumentPrinter服务封装了打印逻辑。 我们配置PaperSize.Legal (8.5 × 14英寸),这是合同和法律文件的标准格式。 await关键字确保调用线程在IronPrint处理打印任务时保持响应。

我们可以从按钮点击处理程序、后台服务或任何支持await的地方调用此方法。 IronPrint的异步方法——包括PrintAsyncShowPrintDialogAsync——接受相同的PrintSettings配置,因此在同步和异步路径之间,纸张尺寸行为是相同的。

我的下一个步骤是什么?

我们讲述了如何使用IronPrint的PaperSize枚举在C#中设置纸张尺寸,从基本的单属性配置到组合设置和异步打印。 PrintSettings类提供了一个干净的、强类型的API,消除了纸张尺寸的猜测。

继续探索 IronPrint 的功能:

立即开始 30天试用,在您的项目中测试纸张尺寸配置,或查看用于生产部署的许可选项

PaperOrientation.Landscape

Dpi

PaperMargins

Margins

Grayscale

false

常见问题解答

什么是IronPrint以及它如何帮助设置C#中的纸张尺寸?

IronPrint是一个简化C#中打印任务的库。它允许开发人员使用直观的代码示例轻松配置A4、Letter和Legal等纸张尺寸。

我可以使用IronPrint在C#中设置自定义纸张尺寸吗?

是的,IronPrint使您能够在C#中为打印设置自定义纸张尺寸。您可以定义符合特定打印要求的尺寸。

如何使用IronPrint将纸张尺寸更改为A4?

要使用IronPrint将纸张尺寸更改为A4,您需要在代码中配置纸张尺寸设置。IronPrint提供了一个简单的方法,以最小的编码努力来设置A4纸张尺寸。

IronPrint支持C#中的Legal纸张尺寸配置吗?

是的,IronPrint支持C#中的Legal纸张尺寸配置。您可以通过调整打印设置代码中的纸张尺寸设置轻松设置为Legal。

是否可以使用IronPrint在不同纸张尺寸之间切换?

绝对可以,IronPrint允许您通过修改C#应用程序中的纸张尺寸设置在A4、Letter和Legal等不同纸张尺寸之间切换。

How does IronPrint handle cases where no paper size is specified?

If no `PaperSize` is specified in the `PrintSettings`, IronPrint defaults to `PaperSize.PrinterDefault`, which uses the paper size configured in the operating system's default printer.

What happens if I want to respect end-user printer preferences in IronPrint?

Using `PaperSize.PrinterDefault` allows IronPrint to respect end-user printer preferences, as it uses the size set in the default printer configuration.

How do I install IronPrint for use in a C# project?

You can install IronPrint via NuGet with the command `Install-Package IronPrint`, and then include `using IronPrint;` in your C# files to access its functionalities.

What are some paper sizes supported by IronPrint?

IronPrint supports several paper sizes including ISO standards like A0, A1, A2, A3, A4, A5, and common US sizes like Letter, Legal, and Executive.

How can I access the full API reference for IronPrint?

The full API reference for IronPrint can be accessed through their documentation at the IronSoftware website, providing detailed information on classes and methods.

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

准备开始了吗?

Nuget Downloads 46,090版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPrint
nuget.org/packages/IronPrint/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择“浏览”并搜索“IronPrint”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并解压 IronPrint 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择“浏览”,“IronPrint.dll”

$999 起售

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户