如何在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. 使用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;

// 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 用設置物件上的一個單一屬性取代整個流程。

我如何將方向與其他列印設置結合?

當方向與紙張大小、DPI 和邊界結合使用時最有用,可以定義一個完整的列印佈局。 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 都以毫米為單位。

下一步

紙張方向是在 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屬性來指定縱向、橫向或自動模式來達到對紙張方向的完全控制。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

仍在滾動?

想要快速證明嗎? PM > Install-Package IronPrint
運行範例 看看您的文件如何到達印表機。