如何在C#中設定列印的紙張方向
紙張方向控制文件以直向(高)或橫向(寬)模式列印。 直向適合大多數的信件、發票和報告。 橫向是寬表格、電子表格、控制面板和簡報幻燈片的最佳選擇。 以程式設置方向可確保不論使用者的預設印表機配置為何,都能獲得一致的輸出。
IronPrint 在 PrintSettings 類別上公開一個 PaperOrientation 屬性。 我們將其設為 Portrait 或 Landscape,將設定傳入 Printer.Print(),文件會以指定的佈局列印。
快速入門:設定紙張方向
- 透過NuGet安裝
Install-Package IronPrint - 將
using IronPrint;新增到檔案 - 建立一個
PrintSettings物件 - 將
PaperOrientation設為Portrait或Landscape - 將設定傳入
Printer.Print()或Printer.ShowPrintDialog()
最小化工作流程(5 步驟)
- 安裝 IronPrint C# 列印程式庫
- 建立一個
PrintSettings物件 - Set
PaperOrientationtoPortraitorLandscape - 將設置傳遞給
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;
// 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)
使用原生 .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;
// 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)
PaperSize 和 PaperOrientation 一起工作 - 設定A4橫向給出297 × 210 毫米列印區域,而A4直向給出210 × 297 毫米。 Dpi 屬性控制輸出解像度(300 是商務文件的標準),PaperMargins 都以毫米為單位。
下一步
紙張方向是在 PrintSettings 物件上的一個屬性 — 將 PaperOrientation 設為 Landscape,或 Automatic,再將其傳給任何 IronPrint 列印方法。 與 Dpi,和 PaperMargins 結合以獲得完整的佈局控制。
探索每個可用屬性的列印設置指導,印表機類API參考以獲得完整的方法表面,或程式碼範例頁面以獲取現成運行的程式碼段。 IronPrint教程涵蓋整個列印生命週期,並變更日誌追踪最近的更新,包括性能改進。
常見問題
如何在C#中設置列印的紙張方向?
要在C#中設置列印的紙張方向,您可以使用IronPrint的PaperOrientation屬性。這使您可以指定是將文件列印為縱向、橫向還是自動方向。
IronPrint中有哪些紙張方向選項?
IronPrint提供選項來將紙張方向設置為縱向、橫向或自動,讓您可以完全控制文件的列印方式。
在IronPrint中是否可以自動確定紙張方向?
是的,IronPrint可以通過其自動方向設置自動確定文件的最佳紙張方向。
在IronPrint中用於控制紙張方向的屬性是什麼?
IronPrint中的PaperOrientation屬性用於控制C#中列印文件的紙張方向。
IronPrint能否處理橫向列印?
是的,IronPrint可以通過將PaperOrientation屬性設置為橫向來處理橫向列印。
IronPrint是否支持文件列印的縱向模式?
IronPrint完全支持文件列印的縱向模式,這可以通過將PaperOrientation屬性設置為縱向來實現。
如何使用IronPrint在C#中達到對紙張方向的完全控制?
您可以通過在IronPrint中利用PaperOrientation屬性來指定縱向、橫向或自動模式來達到對紙張方向的完全控制。

