IRONSOFTWAREHOME

如何在 C# 中设置打印纸张方向

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

纸张方向决定文档是按纵向(高)还是横向(宽)模式打印。 竖版布局适用于大多数信函、发票和报告。 对于宽幅表格、电子表格、仪表盘和演示文稿幻灯片,横向布局是更佳的选择。 通过编程方式设置页面方向,可确保无论用户的默认打印机配置如何,输出效果始终一致。

IronPrintPaperOrientation属性。 我们分别将其设置为Printer.Print(),文档将在指定的布局中打印。

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

    PM > Install-Package IronPrint

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

    using IronPrint;
    
    // Print a document in landscape orientation
    Printer.Print("report.pdf", new PrintSettings
    {
        PaperOrientation = PaperOrientation.Landscape
    });
    C#
  3. 3部署到您的生产环境中进行测试

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

如何设置打印纸张方向?

PaperOrientation上的属性接受三个值:

  • PaperOrientation.Portrait — 竖向布局(大多数打印机的默认设置)。 最适合用于信函、合同和发票等单栏文档。
  • PaperOrientation.Landscape — 横向布局。 最适合处理数据表、甘特图、电子表格和幻灯片等大篇幅内容。
  • PaperOrientation.Automatic — 推迟到打印机的默认设置。

我们创建一个Printer.Print()以实现静默打印Printer.ShowPrintDialog()以实现对话框打印

using IronPrint;

// Configure portrait orientation
var portraitSettings = new PrintSettings
{
    PaperOrientation = PaperOrientation.Portrait
};

// Print the invoice in portrait
Printer.Print("invoice.pdf", portraitSettings);

// Configure landscape orientation
var landscapeSettings = new PrintSettings
{
    PaperOrientation = PaperOrientation.Landscape
};

// Print the dashboard in landscape
Printer.Print("quarterly-dashboard.pdf", landscapeSettings);

使用本地.NETPrintPage事件、图形渲染和手动页面管理。 IronPrint 将整个处理流程替换为设置对象上的一个属性。

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

当与纸张尺寸、DPI 和边距结合使用时,方向设置对于定义完整的打印布局最为有用。 PrintSettings类让我们在一个对象内配置所有这些属性。

using IronPrint;

// Combine orientation with paper size, DPI, and margins
var settings = new PrintSettings
{
    PaperOrientation = PaperOrientation.Landscape,
    PaperSize = PaperSize.A4,
    Dpi = 300,
    NumberOfCopies = 1,
    PaperMargins = new Margins(15, 15, 15, 15),
    Grayscale = false
};

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

PaperOrientation共同工作——设置A4横向提供297 x 210 mm打印区域,而A4竖向提供210 x 297 mm。 PaperMargins值以毫米为单位。

如何在打印对话框中让用户选择页面方向?

当我们将Printer.ShowPrintDialog()时,打开的对话框会显示我们预设的方向。 用户可以在打印前接受该设置,或在纵向和横向模式之间切换。

using IronPrint;

// Pre-configure landscape orientation for the dialog
var settings = new PrintSettings
{
    PaperOrientation = PaperOrientation.Landscape,
    PaperSize = PaperSize.Letter
};

// Open the dialog with pre-selected orientation
Printer.ShowPrintDialog("wide-report.pdf", settings);

对于非阻塞UI场景,异步变体Printer.ShowPrintDialogAsync()接受相同的参数,并在对话框打开时保持应用程序的响应。 此功能在排版方向选择方面尤为实用,因为用户通常希望在正式打印前预览文档在纵向和横向布局下的显示效果。 打印文档教程全面涵盖了无交互和带对话框两种工作流。

下一步

页面方向是Automatic,并将其传递给任何IronPrint打印方法。 将它与PaperMargins结合以实现完整的布局控制。

请查阅各可用属性的打印设置指南Printer 类 API 参考以了解完整的方法接口,或访问代码示例页面获取可直接运行的代码片段。 IronPrint 教程详细介绍了完整的打印生命周期,变更日志则记录了包括性能优化在内的近期更新。

立即开始 30天试用,在实际项目中测试方向设置。 准备好后,查看许可选项,起始于$999。

常见问题解答

如何为C#中的打印设置纸张方向?

要为C#中的打印设置纸张方向,您可以使用IronPrint的PaperOrientation属性。这允许您指定希望文档以纵向、横向或自动方向打印。

IronPrint中有哪些纸张方向选项可用?

IronPrint提供了将纸张方向设置为纵向、横向或自动的选项,让您全面控制文档的打印方式。

IronPrint是否可以自动决定纸张方向?

是的,IronPrint可以使用其自动方向设置来自动确定文档的最佳纸张方向。

在IronPrint中使用哪个属性来控制纸张方向?

IronPrint中的PaperOrientation属性用于控制C#中打印文档的纸张方向。

IronPrint可以处理横向打印吗?

是的,IronPrint可以通过将PaperOrientation属性设置为横向来处理横向打印。

IronPrint是否支持文档打印的纵向模式?

IronPrint通过将PaperOrientation属性设置为纵向,完全支持文档打印的纵向模式。

如何使用IronPrint在C#中完全控制纸张方向?

您可以通过在IronPrint中利用PaperOrientation属性来指定纵向、横向或自动模式,从而完全控制纸张方向。

Does IronPrint support async operations for user interaction in print dialogs?

Yes, IronPrint supports asynchronous operations through `Printer.ShowPrintDialogAsync()`, allowing the application to remain responsive while the print dialog is open, which is useful for scenarios where users need to preview document orientation options.

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 天试用密钥
无需信用卡或创建账户