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

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

紙張方向控制文件以直向(高)或橫向(寬)模式列印。 直向適合大多數的信件、發票和報告。 橫向是寬表格、電子表格、控制面板和簡報幻燈片的最佳選擇。 以程式設置方向可確保不論使用者的預設印表機配置為何,都能獲得一致的輸出。

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

快速入門:設定紙張方向
  1. 透過NuGet安裝 Install-Package IronPrint
  2. 將 using IronPrint; 新增到檔案
  3. 建立一個 PrintSettings 物件
  4. 將 PaperOrientation 設為 Portrait 或 Landscape
  5. 將設定傳入 Printer.Print() 或 Printer.ShowPrintDialog()
  1. 1Install IronPrint with 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

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

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

  • 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 方法,方向是一個布林值 (DefaultPageSettings.Landscape = true),它埋在一個需要 PrintDocument 事件處理、圖形渲染和手動頁面管理的 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 設為 Landscape,或 Automatic,再將其傳給任何 IronPrint 列印方法。 與 Dpi,和 PaperMargins 結合以獲得完整的佈局控制。

探索每個可用屬性的列印設置指導,印表機類API參考以獲得完整的方法表面,或程式碼範例頁面以獲取現成運行的程式碼段。 IronPrint教程涵蓋整個列印生命週期,並變更日誌追踪最近的更新,包括性能改進。

開始免費30天試用以在現場專案中測試方向設定。 準備好後,查看授權選項開始於 $999。

常見問題

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

要在C#中設置列印的紙張方向,您可以使用IronPrint的PaperOrientation屬性。這使您可以指定是將文件列印為縱向、橫向還是自動方向。

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

IronPrint提供選項來將紙張方向設置為縱向、橫向或自動,讓您可以完全控制文件的列印方式。

在IronPrint中是否可以自動確定紙張方向?

是的,IronPrint可以通過其自動方向設置自動確定文件的最佳紙張方向。

在IronPrint中用於控制紙張方向的屬性是什麼?

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

IronPrint能否處理橫向列印?

是的,IronPrint可以通過將PaperOrientation屬性設置為橫向來處理橫向列印。

IronPrint是否支持文件列印的縱向模式?

IronPrint完全支持文件列印的縱向模式,這可以通過將PaperOrientation屬性設置為縱向來實現。

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

您可以通過在IronPrint中利用PaperOrientation屬性來指定縱向、橫向或自動模式來達到對紙張方向的完全控制。

Does IronPrint support async operations for user interaction in print dialogs?

Yes, IronPrint supports asynchronous operations through Printer.ShowPrintDialogAsync(), allowing the application to remain responsive while the print dialog is open, which is useful for scenarios where users need to preview document orientation options.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

閱讀更多

準備好開始了嗎?

Nuget Downloads 49,171版本:2026.11剛剛發布