如何在C#中設置列印紙張大小
IronPrint 的 PrintSettings 類別讓開發者可以透過 PaperSize 屬性直接控制紙張大小。 我們從 PaperSize 列舉中指派一個值,例如 PaperSize.A4 或 PaperSize.Letter,然後將設置好的 PrintSettings 物件傳遞給 IronPrint 的任一列印方法。 然後,列印機會使用該確定的紙張大小進行工作。
這個指南介紹了如何設置標準紙張大小,將大小和其他列印設置結合,異步列印——全部都提供可執行的 C# 程式碼。
快速入門:設置紙張大小- 通過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;新增到文件中 - 建立一個
PrintSettings物件 - 將
PaperSize設置為PaperSize列舉中的任意值(例如,PaperSize.A4) - 將設置傳遞給
Printer.Print()或Printer.PrintAsync()
-
1Install IronPrint with NuGet Package Manager
-
2複製並運行這段程式碼片段。
using IronPrint; // Print a PDF on A4 paper Printer.Print("report.pdf", new PrintSettings { PaperSize = PaperSize.A4 });C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPrint,透過免費試用
最小化工作流程(5 步驟)
- 安裝 IronPrint C# 列印程式庫
- 建立一個
PrintSettings物件 - 設定
PaperSize從PaperSize枚舉中 - 將設置傳遞給
Printer.Print() - 運行專案以在所選紙張大小進行列印
如何在 C# 中設置列印紙張大小?
我們通過將 PaperSize 枚舉中的一個值指派給 PrintSettings 物件上的 PaperSize 屬性來設置紙張大小。 然後我們將該物件傳遞給 IronPrint 的任何 列印方法。
using IronPrint;
// Configure print settings with US Letter paper
PrintSettings settings = new PrintSettings();
settings.PaperSize = PaperSize.Letter;
// Print to the default printer
Printer.Print("invoice.pdf", settings);Imports IronPrint
' Configure print settings with US Letter paper
Dim settings As New PrintSettings()
settings.PaperSize = PaperSize.Letter
' Print to the default printer
Printer.Print("invoice.pdf", settings)我們首先實例化 PrintSettings,它會以列印機的預設值初始化。 接著,我們用 PaperSize.Letter 覆蓋 PaperSize,該值對應於標準的8.5×11英寸格式。 當我們呼叫 Printer.Print 時,IronPrint 使用該紙張大小將文件發送到系統的預設列印機。
如果沒有指定 PaperSize,IronPrint 使用 PaperSize.PrinterDefault,這將推遲到操作系統設置的預設列印機所使用的紙張大小。 這對於在生產環境中,各台機器的列印機配置各異的情況下是很重要的細節。
程式庫支持哪種紙張大小?
PaperSize 列舉包含了涵蓋ISO國際標準、常見美國尺寸以及列印機預設值的十二個值。 下表列出了所有可用的選項。
| 列舉值 | 標準 | 尺寸(毫米) | 尺寸(英寸) |
|---|---|---|---|
PaperSize.A0 | ISO A0 | 841 × 1189 | 33.1 × 46.8 |
PaperSize.A1 | ISO A1 | 594 × 841 | 23.4 × 33.1 |
PaperSize.A2 | ISO A2 | 420 × 594 | 16.5 × 23.4 |
PaperSize.A3 | ISO A3 | 297 × 420 | 11.7 × 16.5 |
PaperSize.A4 | ISO A4 | 210 × 297 | 8.3 × 11.7 |
PaperSize.A5 | ISO A5 | 148 × 210 | 5.8 × 8.3 |
PaperSize.B4 | ISO B4 | 250 × 353 | 9.8 × 13.9 |
PaperSize.B5 | ISO B5 | 176 × 250 | 6.9 × 9.8 |
PaperSize.Letter | 美國信紙 | 216 × 279 | 8.5 × 11.0 |
PaperSize.Legal | 美國法律文件 | 216 × 356 | 8.5 × 14.0 |
PaperSize.Executive | 美國行政 | 184 × 267 | 7.25 × 10.5 |
PaperSize.PrinterDefault | 列印機預設 | 依情況而異 | 依情況而異 |
每個值都直接對應於一個已知的紙張標準。 PrinterDefault 選項指示 IronPrint 使用列印機當前配置的大小——在您想尊重最終使用者的列印機偏好而非強制某種格式時很有用。
欲查閱完整的 API 參考,請參閱 PaperSize 類文件。
如何將紙張大小與其他列印設置結合?
PrintSettings 類別揭示了除了 PaperSize 之外的若干屬性。 我們可以配置 方向、DPI、邊界、複製次數及灰階模式——所有設定在同一物件中。
using IronPrint;
// Configure full print settings
PrintSettings settings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape,
Dpi = 300,
NumberOfCopies = 3,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
// Print the quarterly report
Printer.Print("quarterly-report.pdf", settings);Imports IronPrint
' Configure full print settings
Dim settings As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape,
.Dpi = 300,
.NumberOfCopies = 3,
.PaperMargins = New Margins(15, 15, 15, 15),
.Grayscale = False
}
' Print the quarterly report
Printer.Print("quarterly-report.pdf", settings)為了清楚起見,我們在此使用物件初始化語法。 Orientation 將 A4 紙張旋轉至其寬軸。 300 的 DPI 確保對於圖表和細文字的清晰輸出。 Margins 接受通過 Margins 構造函式的四個值(以毫米為單位)——上、右、下、左。 雖然 CopyCount 屬性預設為 1,但我們明確地設置它來提高可讀性。
這些屬性可以無衝突地一起運作。 IronPrint 驗證配置並將合併設置傳遞給列印機驅動作為單一列印工作。 若要獲得更高級的設置,例如 列印機選擇 和 紙匣配置,請參見完整的 列印設置指南。
如何使用自定義紙張大小進行異步列印?
對於那些無法阻塞主執行緒的應用程式,例如 WPF 或 WinForms 應用,我們使用 Printer.PrintAsync。 該方法接受相同的 PrintSettings 物件並返回一個 Task。
using IronPrint;
using System.Threading.Tasks;
public class DocumentPrinter
{
public async Task PrintLegalDocumentAsync(string filePath)
{
// Configure Legal paper size
PrintSettings settings = new PrintSettings
{
PaperSize = PaperSize.Legal,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300
};
// Print asynchronously
await Printer.PrintAsync(filePath, settings);
}
}Imports IronPrint
Imports System.Threading.Tasks
Public Class DocumentPrinter
Public Async Function PrintLegalDocumentAsync(filePath As String) As Task
' Configure Legal paper size
Dim settings As New PrintSettings With {
.PaperSize = PaperSize.Legal,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300
}
' Print asynchronously
Await Printer.PrintAsync(filePath, settings)
End Function
End Class此基於類別的範例展示了一種現實模式,其中 DocumentPrinter 服務包裹列印邏輯。 我們設置 PaperSize.Legal (8.5 × 14 英寸),這是合同和法律文件的標準格式。 await 關鍵字確保在 IronPrint 處理列印工作的同時,調用執行緒保持響應。
我們可以從按鈕點擊處理器、背景服務或任何支援 await 的地方呼叫這個方法。 IronPrint 的異步方法——包括 PrintAsync 和 ShowPrintDialogAsync——接受相同的 PrintSettings 配置,因此在同步和異步路徑之間紙張大小的行為是相同的。
我的下一步是什麼?
我們涵蓋了如何在 C# 中使用 IronPrint 的 PaperSize 枚舉設置紙張大小,從基本的單屬性配置到合併設置及異步列印。 PrintSettings 類別提供了一個乾淨且強型別的 API,消除了對紙張尺寸的猜測。
要繼續探索 IronPrint 的功能:
開始 30 天免費試用以在您自己的專案中測試紙張大小配置,或者查看部署到生產環境的授權選項。
PaperOrientation.Landscape
Dpi
PaperMargins
Margins
Grayscale
false
常見問題
IronPrint是什麼,它如何幫助在C#中設定列印的紙張大小?
IronPrint是一個簡化C#中列印任務的程式庫。它允許開發者使用簡單的程式碼範例輕鬆配置諸如A4、信紙、法律紙等紙張尺寸。
我可以在C#中使用IronPrint設置自定義紙張尺寸嗎?
可以,IronPrint使您可以在C#中設置列印的自定義紙張尺寸。您可以定義符合您特定列印需求的尺寸。
如何使用IronPrint將紙張大小更改為A4?
要使用IronPrint將紙張大小更改為A4,您需要在程式碼中配置紙張大小設置。IronPrint提供了一個簡單的方法來將紙張大小設定為A4,僅需少量編碼工作。
IronPrint是否支持法律紙尺寸配置?
支持,IronPrint在C#中支持法律紙尺寸配置。您可以在您的列印設置程式碼中通過調整紙張大小設置輕鬆設置為法律紙大小。
是否可以使用IronPrint在不同紙張尺寸之間切換?
絕對可以,IronPrint允許您通過修改C#應用程式中的紙張大小設置在不同的紙張尺寸(如A4、信紙和法律紙)之間切換。
How does IronPrint handle cases where no paper size is specified?
If no `PaperSize` is specified in the `PrintSettings`, IronPrint defaults to `PaperSize.PrinterDefault`, which uses the paper size configured in the operating system's default printer.
What happens if I want to respect end-user printer preferences in IronPrint?
Using `PaperSize.PrinterDefault` allows IronPrint to respect end-user printer preferences, as it uses the size set in the default printer configuration.
How do I install IronPrint for use in a C# project?
You can install IronPrint via NuGet with the command `Install-Package IronPrint`, and then include `using IronPrint;` in your C# files to access its functionalities.
What are some paper sizes supported by IronPrint?
IronPrint supports several paper sizes including ISO standards like A0, A1, A2, A3, A4, A5, and common US sizes like Letter, Legal, and Executive.
How can I access the full API reference for IronPrint?
The full API reference for IronPrint can be accessed through their documentation at the IronSoftware website, providing detailed information on classes and methods.

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