如何在 C# 中设置打印纸张方向
纸张方向决定文档是按纵向(高)还是横向(宽)模式打印。 竖版布局适用于大多数信函、发票和报告。 对于宽幅表格、电子表格、仪表盘和演示文稿幻灯片,横向布局是更佳的选择。 通过编程方式设置页面方向,可确保无论用户的默认打印机配置如何,输出效果始终一致。
IronPrint 在 PrintSettings 类上公开了一个 PaperOrientation 属性。 我们将其设置为 Portrait 或 Landscape,将设置传递给 Printer.Print(),文档即可按指定布局打印。
- 通过NuGet安装
IronPrint:Install-Package IronPrint - 将
using IronPrint;添加到文件中 - 创建
PrintSettings对象 - 将
PaperOrientation设置为Portrait或Landscape - 将设置传递给
Printer.Print()或Printer.ShowPrintDialog()
-
1使用 IronPrint 安装 NuGet Package Manager
-
2复制并运行这段代码。
using IronPrint; // Print a document in landscape orientation Printer.Print("report.pdf", new PrintSettings { PaperOrientation = PaperOrientation.Landscape });Imports IronPrint ' Print a document in landscape orientation Printer.Print("report.pdf", New PrintSettings With { .PaperOrientation = PaperOrientation.Landscape })Imports IronPrint ' Print a document in landscape orientation Printer.Print("report.pdf", New PrintSettings With { .PaperOrientation = PaperOrientation.Landscape }) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 创建一个
PrintSettings对象 - 将
PaperOrientation设置为Portrait或Landscape - 将设置传递给
Printer.Print() - 运行该项目以指定方向进行打印
如何设置打印纸张方向?
PaperOrientation 属性在 PrintSettings 上接受三个值:
PaperOrientation.Portrait- 纵向布局(大多数打印机的默认设置)。 最适合用于信函、合同和发票等单栏文档。PaperOrientation.Landscape- 横向布局。 最适合处理数据表、甘特图、电子表格和幻灯片等大篇幅内容。PaperOrientation.Automatic- 遵循打印机的默认设置。
我们创建一个 PrintSettings 对象,分配所需的方向,并将其传递给 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);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)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)使用原生 .NET System.Drawing.Printing 方法,方向是一个隐藏在 PrintDocument 中的布尔值 (DefaultPageSettings.Landscape = true),还需要 PrintPage 事件处理、图形渲染和手动页面管理。 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);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)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)PaperSize 和 PaperOrientation 协同工作——设置A4横向提供297 × 210毫米的打印区域,而A4纵向提供210 × 297毫米。 Dpi属性控制输出分辨率(300 是商业文档的标准),而PaperMargins的值是以毫米为单位。
如何在打印对话框中让用户选择页面方向?
当我们将 PrintSettings 传递给 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);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);Imports IronPrint
' Pre-configure landscape orientation for the dialog
Dim settings As New PrintSettings With {
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.Letter
}
' Open the dialog with pre-selected orientation
Printer.ShowPrintDialog("wide-report.pdf", settings)Imports IronPrint
' Pre-configure landscape orientation for the dialog
Dim settings As New PrintSettings With {
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.Letter
}
' Open the dialog with pre-selected orientation
Printer.ShowPrintDialog("wide-report.pdf", settings)对于非阻塞UI场景,异步变体Printer.ShowPrintDialogAsync()接受相同的参数,并在对话框打开时保持应用程序响应。 此功能在排版方向选择方面尤为实用,因为用户通常希望在正式打印前预览文档在纵向和横向布局下的显示效果。 打印文档教程详细介绍无声和对话框工作流程。
下一步
纸张方向是 PrintSettings 对象的一个属性——将 PaperOrientation 设置为 Portrait, Landscape, 或 Automatic,并传递给任何 IronPrint 打印方法。 将其与 PaperSize, Dpi, 和 PaperMargins 结合使用以实现完全的布局控制。
探索打印设置方法了解每个可用属性、Printer类API参考获取完整的方法界面,或代码示例页面获取即用的代码片段。 IronPrint教程完整讲解打印生命周期,而更新日志记录最近的更新,包括性能改进。
开始免费30天试用在实时项目中测试方向设置。 准备好后,查看许可选项,价格从 $999 开始。
常见问题
如何在C#中设置打印的纸张方向?
要在C#中使用IronPrint设置纸张方向,请在PrintSettings类上使用PaperOrientation属性。您可以将其设置为Portrait以实现纵向布局,或设置为Landscape以实现横向布局。将这些设置传递给Printer.Print()以确保文档以指定的方向打印。
IronPrint中有哪些可用的纸张方向选项?
IronPrint提供三种PaperOrientation选项:Portrait、Landscape和Automatic。Portrait适合如信件这样的纵向文件,而Landscape则适合如电子表格这样的横向内容。Automatic将遵循打印机的默认方向设置。
我可以在设置纸张方向的同时修改其他打印设置吗?
可以,IronPrint允许您通过在同一PrintSettings对象内配置,结合方向与其他设置如纸张大小、DPI和边距。这确保了对打印布局的全面控制。
是否可以通过打印对话框让用户选择其首选方向?
可以,通过使用Printer.ShowPrintDialog()和预配置的PrintSettings对象,用户可以在对话框中选择纵向和横向方向。
如何安装IronPrint以管理项目中的纸张方向?
要安装IronPrint,请使用NuGet命令Install-Package IronPrint。这将把所需的库添加到您的项目中,使您能够控制纸张方向和其他打印设置。
IronPrint在设置纸张方向上的方法与.NET本机的System.Drawing.Printing有何不同?
IronPrint通过在其PrintSettings对象中使用PaperOrientation属性,取代了处理PrintPage事件和管理页面布局等复杂的本机方法,从而简化了流程,提供了一种更简单、更高效的设定方向方式。
如何开始IronPrint的试用以测试方向设置?
您可以访问Iron Software网站,开始免费30天试用IronPrint。这使您能够在实际项目中探索和测试不同的方向设置和其他功能,然后再决定购买。
IronPrint是否支持打印对话框中的用户交互异步操作?
是的,IronPrint通过Printer.ShowPrintDialogAsync()支持异步操作,使应用程序在打印对话框打开时保持响应,这对用户需要预览文档方向选项的场景非常有用。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。