如何在 C# 中设置打印纸张方向
纸张方向决定文档是按纵向(高)还是横向(宽)模式打印。 竖版布局适用于大多数信函、发票和报告。 对于宽幅表格、电子表格、仪表盘和演示文稿幻灯片,横向布局是更佳的选择。 通过编程方式设置页面方向,可确保无论用户的默认打印机配置如何,输出效果始终一致。
IronPrint 在 PrintSettings 类上提供了 PaperOrientation 属性。 我们将它设置为 Portrait 或 Landscape,将设置传递给 Printer.Print(),文档便会按指定布局打印出来。
快速入门:设置纸张方向
- 通过 NuGet 安装 IronPrint:
Install-Package IronPrint - 在文件中添加
using IronPrint; - 创建一个
PrintSettings对象 - 将
PaperOrientation设置为Portrait或Landscape - 将设置传递给
Printer.Print()或Printer.ShowPrintDialog()
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
复制并运行这段代码。
using IronPrint; // Print a document in landscape orientation Printer.Print("report.pdf", new PrintSettings { PaperOrientation = PaperOrientation.Landscape }); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
PaperOrientation设置为Portrait或Landscape - 将设置传递给
Printer.Print() - 运行该项目以指定方向进行打印
如何设置打印纸张方向?
PrintSettings 中的 PaperOrientation 属性接受三个值:
PaperOrientation.Portrait— 纵向布局(大多数打印机的默认设置)。 最适合用于信函、合同和发票等单栏文档。PaperOrientation.Landscape— 水平布局。 最适合处理数据表、甘特图、电子表格和幻灯片等大篇幅内容。PaperOrientation.Automatic— 采用打印机的默认设置。
我们创建一个 PrintSettings 对象,为其指定所需的页面方向,然后将其传递给 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;
// Portrait orientation — standard for letters and invoices
var portraitSettings = new PrintSettings
{
PaperOrientation = PaperOrientation.Portrait
};
Printer.Print("invoice.pdf", portraitSettings);
// Landscape orientation — ideal for wide tables and dashboards
var landscapeSettings = new PrintSettings
{
PaperOrientation = PaperOrientation.Landscape
};
Printer.Print("quarterly-dashboard.pdf", landscapeSettings);
Imports IronPrint
' Portrait orientation — standard for letters and invoices
Dim portraitSettings As New PrintSettings With {
.PaperOrientation = PaperOrientation.Portrait
}
Printer.Print("invoice.pdf", portraitSettings)
' Landscape orientation — ideal for wide tables and dashboards
Dim landscapeSettings As New PrintSettings With {
.PaperOrientation = PaperOrientation.Landscape
}
Printer.Print("quarterly-dashboard.pdf", landscapeSettings)
使用原生 .NET System.Drawing.Printing 方法时,纸张方向是一个布尔值 (DefaultPageSettings.Landscape = true),该值被封装在 PrintDocument 结构中,且需要 PrintPage 事件处理、图形渲染以及手动页面管理。 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;
var settings = new PrintSettings
{
PaperOrientation = PaperOrientation.Landscape,
PaperSize = PaperSize.A4,
Dpi = 300,
NumberOfCopies = 1,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
Printer.Print("financial-report.pdf", settings);
Imports IronPrint
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
}
Printer.Print("financial-report.pdf", settings)
PaperSize 和 PaperOrientation 配合使用——设置为 A4 横向时,打印区域为 297 × 210 毫米;而 A4 纵向时,打印区域为 210 × 297 毫米。 Dpi 属性控制输出分辨率(300 是商务文档的标准值),而 PaperMargins 的数值单位为毫米。
如何在打印对话框中让用户选择页面方向?
当我们将 PrintSettings 传递给 Printer.ShowPrintDialog() 时,对话框将以预设的页面方向打开。 用户可以在打印前接受该设置,或在纵向和横向模式之间切换。
:path=/static-assets/print/content-code-examples/how-to/set-paper-orientation/set-paper-orientation-dialog-with-orientation-preset.cs
using IronPrint;
// Pre-select landscape, but let the user override in the dialog
var settings = new PrintSettings
{
PaperOrientation = PaperOrientation.Landscape,
PaperSize = PaperSize.Letter
};
Printer.ShowPrintDialog("wide-report.pdf", settings);
Imports IronPrint
' Pre-select landscape, but let the user override in the dialog
Dim settings As New PrintSettings With {
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.Letter
}
Printer.ShowPrintDialog("wide-report.pdf", settings)
对于非阻塞的 UI 场景,异步版本 Printer.ShowPrintDialogAsync() 接受相同的参数,并在对话框打开期间保持应用程序的响应性。 此功能在排版方向选择方面尤为实用,因为用户通常希望在正式打印前预览文档在纵向和横向布局下的显示效果。 打印文档教程全面涵盖了无交互和带对话框两种工作流。
下一步
纸张方向是 PrintSettings 对象的一个属性——将 PaperOrientation 设置为 Landscape 或 Automatic,并将其传递给任何 IronPrint 打印方法。 请与 Dpi 和 PaperMargins 结合使用,以实现完整的版面控制。
请查阅各可用属性的打印设置指南、Printer 类 API 参考以了解完整的方法接口,或访问代码示例页面获取可直接运行的代码片段。 IronPrint 教程详细介绍了完整的打印生命周期,变更日志则记录了包括性能优化在内的近期更新。
立即开始 30天试用,在实际项目中测试方向设置。 准备就绪后,请查看起价为 749 美元的许可方案。

