如何在 C# 中设置打印纸张方向
纸张方向决定文档是按纵向(高)还是横向(宽)模式打印。 竖版布局适用于大多数信函、发票和报告。 对于宽幅表格、电子表格、仪表盘和演示文稿幻灯片,横向布局是更佳的选择。 通过编程方式设置页面方向,可确保无论用户的默认打印机配置如何,输出效果始终一致。
IronPrint在PaperOrientation属性。 我们分别将其设置为Printer.Print(),文档将在指定的布局中打印。
快速入门:设置纸张方向
- 通过NuGet安装
IronPrint:Install-Package IronPrint - 添加
using IronPrint;到文件 - 创建一个
PrintSettings对象 - 将
Landscape - 将设置传递给
Printer.ShowPrintDialog()
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
PaperOrientation设置为纵向或横向 - 将设置传递给
Printer.Print() - 运行该项目以指定方向进行打印
如何设置打印纸张方向?
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)
使用本地.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)
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属性来指定纵向、横向或自动模式,从而完全控制纸张方向。

