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

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

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

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

快速入門:設定紙張方向

  1. 透過 NuGet 安裝 Install-Package IronPrint
  2. using IronPrint; 加入檔案中
  3. 建立一個 PrintSettings 物件
  4. PaperOrientation 設定為 PortraitLandscape
  5. 將設定傳遞至 Printer.Print()Printer.ShowPrintDialog()
  1. using 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

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

PaperOrientation 屬性在 PrintSettings 上接受三個值:

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

採用原生 .NET System.Drawing.Printing 方法時,方向資訊是一個布林值 (DefaultPageSettings.Landscape = true),被封裝在 PrintDocument 結構中,且還需處理 PrintPage 事件、圖形渲染以及手動頁面管理。 IronPrint 將整個處理流程替換為設定物件上的單一屬性。

如何將紙張方向與其他PRINT設定結合使用?

當與紙張尺寸、DPI 及邊距結合使用時,方向設定對於定義完整的 PRINT 版面配置最為有用。 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)
$vbLabelText   $csharpLabel

PaperSizePaperOrientation 相互配合運作 — 設定為 A4 橫向時,列印區域為 297 × 210 公釐;而 A4 縱向則為 210 × 297 公釐。 Dpi 屬性用於控制輸出解析度(300 為商務文件的標準值),而 PaperMargins 的數值單位為公釐。

如何讓使用者在PRINT對話方塊中選擇頁面方向?

當我們將 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-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)
$vbLabelText   $csharpLabel

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

後續步驟

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

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

立即開始 30天試用,在實際專案中測試方向設定。 準備就緒後,請參閱自 $999 起提供的授權選項

常見問題

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

若要在 C# 中設定列印的紙張方向,您可以使用 IronPrint 的 PaperOrientation 屬性。此屬性可讓您指定文件應以直向、橫向或自動方向列印。

IronPrint 提供哪些紙張方向選項?

IronPrint 提供將紙張方向設定為直向、橫向或自動的選項,讓您能完全掌控文件的列印方式。

IronPrint 能否自動判別紙張方向?

是的,IronPrint 可透過其自動方向設定功能,為您的文件自動判定最佳的紙張方向。

IronPrint 中用於控制紙張方向的屬性為何?

IronPrint 中的 PaperOrientation 屬性用於在 C# 中控制文件列印時的紙張方向。

IronPrint 能否處理橫向列印?

是的,IronPrint 可透過將 PaperOrientation 屬性設定為 landscape 來處理橫向列印。

IronPrint 是否支援文件列印的直向模式?

IronPrint 透過將 PaperOrientation 屬性設定為「直向」,全面支援文件列印的直向模式。

如何使用 IronPrint 在 C# 中完全控制紙張方向?

您可以透過 IronPrint 中的 PaperOrientation 屬性,指定直向、橫向或自動模式,從而完全掌控紙張方向。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 41,154 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPrint
執行範例程式,親眼見證您的文件送印。