如何在 C# 中設定列印的紙張方向
紙張方向決定文件是以直式(縱向)或橫式(橫向)模式列印。 直式排版適用於大多數信函、發票及報告。 橫向排版更適合寬幅表格、試算表、儀表板及簡報投影片。 透過程式設定列印方向,可確保輸出結果始終一致,不受使用者預設印表機設定的影響。
IronPrint 在 PrintSettings 類別上公開了一個 PaperOrientation 屬性。 我們將其設定為 Portrait 或 Landscape,將設定傳遞至 Printer.Print(),文件便會以指定的版面配置列印出來。
快速入門:設定紙張方向
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 將
PaperOrientation設定為Portrait或Landscape - 將設定傳遞至
Printer.Print()或Printer.ShowPrintDialog()
最小工作流程(5 個步驟)
- 安裝 IronPrint C# 程式庫
- 建立一個 `PrintSettings` 物件
- 將 `PaperOrientation` 設為`直向`或`橫向`
- 將設定傳遞給 `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)
針對非阻塞式使用者介面情境,非同步版本 Printer.ShowPrintDialogAsync() 接受相同的參數,並能在對話方塊開啟期間維持應用程式的回應能力。 此功能對於排版方向的確認特別有用,因為使用者在決定正式列印前,通常希望預覽文件在直向與橫向顯示下的樣貌。 此文件列印教學完整涵蓋了無提示視窗與對話框兩種工作流程。
後續步驟
紙張方向是 PrintSettings 物件的其中一個屬性 — 將 PaperOrientation 設定為 Landscape 或 Automatic,並將其傳遞給任何 IronPrint 列印方法。 請與 Dpi 及 PaperMargins 結合使用,以實現完整的版面配置控制。
請參閱各項可用屬性的列印設定操作指南、Printer 類別的 API 參考以了解完整的方法介面,或瀏覽程式碼範例頁面以取得可直接執行的程式碼片段。 IronPrint 的教學指南完整介紹了列印的整個生命週期,而更新日誌則記錄了近期更新內容,包括效能提升等。
立即開始 30天試用,在實際專案中測試方向設定。 準備就緒後,請查看起價 $749 的授權方案。

