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

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

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

快速入门:设置纸张方向

  1. 通过NuGet安装IronPrint: Install-Package IronPrint
  2. 添加using IronPrint;到文件
  3. 创建一个PrintSettings对象
  4. Landscape
  5. 将设置传递给Printer.ShowPrintDialog()
  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 复制并运行这段代码。

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

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

    arrow pointer

如何设置打印纸张方向?

PaperOrientation上的属性接受三个值:

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

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

:path=/static-assets/print/content-code-examples/how-to/set-paper-orientation/set-paper-orientation-portrait-and-landscape-orientation.cs
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);
Imports IronPrint

' Configure portrait orientation
Dim portraitSettings As New PrintSettings With {
    .PaperOrientation = PaperOrientation.Portrait
}

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

' Configure landscape orientation
Dim landscapeSettings As New PrintSettings With {
    .PaperOrientation = PaperOrientation.Landscape
}

' Print the dashboard in landscape
Printer.Print("quarterly-dashboard.pdf", landscapeSettings)
$vbLabelText   $csharpLabel

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

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

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

:path=/static-assets/print/content-code-examples/how-to/set-paper-orientation/set-paper-orientation-combine-orientation-with-settings.cs
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);
Imports IronPrint

' Combine orientation with paper size, DPI, and margins
Dim settings As New PrintSettings With {
    .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)
$vbLabelText   $csharpLabel

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

下一步

页面方向是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属性来指定纵向、横向或自动模式,从而完全控制纸张方向。

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 44,051 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPrint
运行示例 观看您的文档打到打印机上。