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

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

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

IronPrint 在 PrintSettings 類別上公開了一個 PaperOrientation 屬性。 我們將其設置為 Portrait 或 Landscape,將設置傳遞給 Printer.Print(),文件將以指定的佈局列印。

快速入門:設定紙張方向
  1. 通過NuGet安裝IronPrint:Install-Package IronPrint
  2. 將 using IronPrint; 添加到文件中
  3. 建立一個 PrintSettings 物件
  4. 將 PaperOrientation 設置為 Portrait 或 Landscape
  5. 將設置傳遞給 Printer.Print() 或 Printer.ShowPrintDialog()
  1. 1使用 IronPrint 安裝 NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2複製並運行這段程式碼。

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

    立即在您的項目中開始使用 IronPrint,並使用 免費試用
    arrow pointer

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

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);

透過原生 .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);

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);

對於非阻塞 UI 場景,異步變體 Printer.ShowPrintDialogAsync() 接受相同的參數並在對話框打開時保持應用程式的響應。 此功能對於排版方向的確認特別有用,因為使用者在決定正式列印前,通常希望預覽文件在直向與橫向顯示下的樣貌。 列印文件教學涵蓋了從頭到尾的靜默和對話工作流程。

後續步驟

紙張方向是PrintSettings對象上的一個屬性 - 將PaperOrientation設置為Portrait、Landscape或Automatic,並將其傳遞給任何IronPrint打印方法。 將其與PaperSize、Dpi和PaperMargins結合使用,以實現完整的佈局控制。

探索列印設定教學以了解所有可用屬性、印表機類別 API 參考以獲得完整的方法表面,或程式碼示例頁面以獲取現成的代碼片段。 IronPrint 教學會完整指導整個列印生命週期,而更新日誌會跟蹤最近的更新,包括性能改進。

開始免費 30 天試用以在實時專案中測試方向設置。 準備好時,查看授權選項,起價 $999。

常見問題

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

要在C#中使用IronPrint設置紙張方向,請在PrintSettings類中使用PaperOrientation屬性。您可以將其設置為Portrait以獲得縱向佈局,或設置為Landscape以獲得橫向佈局。將這些設置傳遞給Printer.Print()以確保文件按照指定的方向列印。

IronPrint中可用的紙張方向選項有哪些?

IronPrint提供三種PaperOrientation選項:Portrait、Landscape和Automatic。Portrait最適合像信件這樣的縱向文件,而Landscape則適合像試算表這樣的橫向內容。Automatic會預設為印表機的默認方向設置。

我可以在設置紙張方向的同時修改其他列印設置嗎?

可以,IronPrint允許您將方向與其他設置(如紙張大小、DPI和邊距)結合在同一個PrintSettings物件中進行配置。這樣可以確保完全控制列印佈局。

是否可以通過列印對話框讓用戶選擇他們偏好的方向?

可以,使用Printer.ShowPrintDialog()並配合預配置的PrintSettings物件,用戶會看到一個對話框,可以在縱向和橫向之間進行選擇。

我如何安裝IronPrint以管理專案中的紙張方向?

要安裝IronPrint,請使用NuGet並執行命令Install-Package IronPrint。這將把必需的程式庫添加到您的專案中,使您能夠控制紙張方向和其他列印設置。

IronPrint設置紙張方向的方法與.NET的本機System.Drawing.Printing有何不同?

IronPrint通過在其PrintSettings物件中使用PaperOrientation屬性,簡化了過程,無需處理PrintPage事件和管理頁面佈局的復雜本機方法,提供了一種更簡單、更有效的設置方向的方法。

我如何開始IronPrint的試用以測試方向設置?

您可以通過訪問他們的網站開始IronPrint的免費30天試用。這允許您在購買之前,在實際專案中探索和測試不同的方向設置及其他功能。

IronPrint是否支持在列印對話框中的用戶互動的異步操作?

支持,IronPrint通過Printer.ShowPrintDialogAsync()支持異步操作,在列印對話框開啟期間應用程式仍然保持響應,這在用戶需要預覽文件方向選項的情況下非常有用。

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

閱讀更多

準備好開始了嗎?

NuGet 下載次數 52,427版本:2026.11剛剛發布