如何在 C# 中設定列印的紙張方向

This article was translated from English: Does it need improvement?
Translated
View the article in English

紙張方向決定文件是以直式(縱向)或橫式(橫向)模式列印。 直式排版適用於大多數信函、發票及報告。 橫向排版更適合寬幅表格、試算表、儀表板及簡報投影片。 透過程式設定列印方向,可確保輸出結果始終一致,不受使用者預設印表機設定的影響。

IronPrintPrintSettings 類別上公開了一個 PaperOrientation 屬性。 我們將其設定為 PortraitLandscape,將設定傳遞至 Printer.Print(),文件便會以指定的版面配置列印出來。

快速入門:設定紙張方向

  1. 透過 NuGet 安裝 IronPrint:Install-Package IronPrint
  2. using IronPrint; 加入檔案中
  3. 建立一個 PrintSettings 物件
  4. PaperOrientation 設定為 PortraitLandscape
  5. 將設定傳遞至 Printer.Print()Printer.ShowPrintDialog()
  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 複製並運行這段程式碼。

    using IronPrint;
    
    // Print a document in landscape orientation
    Printer.Print("report.pdf", new PrintSettings
    {
        PaperOrientation = PaperOrientation.Landscape
    });
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPrint

    arrow pointer

如何設定列印的紙張方向?

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)
$vbLabelText   $csharpLabel

使用原生的 .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)
$vbLabelText   $csharpLabel

PaperSizePaperOrientation 相互配合 — 設定為 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)
$vbLabelText   $csharpLabel

針對非阻塞式使用者介面情境,非同步版本 Printer.ShowPrintDialogAsync() 接受相同的參數,並能在對話方塊開啟期間維持應用程式的回應能力。 此功能對於排版方向的確認特別有用,因為使用者在決定正式列印前,通常希望預覽文件在直向與橫向顯示下的樣貌。 此文件列印教學完整涵蓋了無提示視窗與對話框兩種工作流程。

後續步驟

紙張方向是 PrintSettings 物件的其中一個屬性 — 將 PaperOrientation 設定為 LandscapeAutomatic,並將其傳遞給任何 IronPrint 列印方法。 請與 DpiPaperMargins 結合使用,以實現完整的版面配置控制。

請參閱各項可用屬性的列印設定操作指南Printer 類別的 API 參考以了解完整的方法介面,或瀏覽程式碼範例頁面以取得可直接執行的程式碼片段。 IronPrint 教學指南完整介紹了列印的整個生命週期,而更新日誌則記錄了近期更新內容,包括效能提升等。

立即開始 30天試用,在實際專案中測試方向設定。 準備就緒後,請查看起價 $749 的授權方案

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 38,930 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPrint
執行範例 觀看您的文件打到印表機上。