如何在 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()
-
1使用 IronPrint 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronPrint; // Print a PDF on A4 paper Printer.Print("report.pdf", new PrintSettings { PaperSize = PaperSize.A4 });Imports IronPrint ' Print a PDF on A4 paper Printer.Print("report.pdf", New PrintSettings With { .PaperSize = PaperSize.A4 })Imports IronPrint ' Print a PDF on A4 paper Printer.Print("report.pdf", New PrintSettings With { .PaperSize = PaperSize.A4 }) -
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);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)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 列舉包括 12 種值,涵蓋了 ISO 國際標準、常見的美國尺寸和印表機預設。 下表列出了所有可用的選項。
| 枚舉值 | 標準 | 尺寸 (mm) | 尺寸(英吋) |
|---|---|---|---|
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);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)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)此處為求清晰起見,採用物件初始化語法。 PaperOrientation 將 A4 紙旋轉到寬軸。 Dpi 在 300 時保證圖表和精細文字的清晰輸出。 Margins 接受四個值(以毫米為單位)- 上、右、下、左 - 通過 Margins 建構函式。 NumberOfCopies 屬性預設為 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);
}
}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 ClassImports 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 處理列印工作時保持響應。
我們可以從按鈕點擊處理程序、背景服務或任何支援async/await 的地方呼叫此方法。 IronPrint 的非同步方法(包括 PrintAsync 和 ShowPrintDialogAsync)接受相同的 PrintSettings 配置,因此紙張尺寸行為在同步和非同步路徑之間是相同的。
接下來我該怎麼做?
我們介紹了如何使用 IronPrint 的 PaperSize 列舉在 C# 中設置紙張尺寸,涵蓋了從基本單屬性配置到組合設定和異步列印。 PrintSettings 類別提供了一個乾淨、強類型的 API,消除了對紙張尺寸的猜測。
若要進一步探索 IronPrint 的功能:
開始免費 30 天試用 以在您自己的專案中測試紙張尺寸配置,或 查看許可選項 以進行生產部署。
常見問題
如何在C#中使用IronPrint設定特定的列印紙張大小?
要在C#中設定特定的紙張大小,使用IronPrint的PrintSettings類。從PaperSize列舉中分配一個值,如PaperSize.A4或PaperSize.Letter,至PaperSize屬性,並將PrintSettings對象傳遞給Printer.Print方法。
IronPrint可以處理多種紙張大小嗎?
是的,IronPrint通過PaperSize列舉支持多種紙張大小,包括A4、Letter、Legal等標準大小。您可以根據列印需求選擇其中任何一種。
IronPrint可以非同步列印文件嗎?
是的,IronPrint提供Printer.PrintAsync方法以非同步列印文件,允許執行非阻塞操作,這對於像WPF或WinForms這樣的應用程式至關重要。
使用IronPrint列印時哪個屬性控制紙張大小?
PrintSettings類中的PaperSize屬性控制使用IronPrint時的紙張大小。
IronPrint允許自定義列印設置超越紙張大小嗎?
是的,IronPrint允許進一步自定義列印設置,包括方向、DPI、邊距、複印數量和灰階模式,通過PrintSettings類。
IronPrint如何處理未指定紙張大小的情況?
如果在PrintSettings中未指定PaperSize,IronPrint將默認為PaperSize.PrinterDefault,使用操作系統默認印表機中配置的紙張大小。
如果我想在IronPrint中尊重最終使用者的印表機偏好會怎麼樣?
使用PaperSize.PrinterDefault允許IronPrint尊重最終用戶的印表機偏好,因為它使用默認印表機配置中的大小。
如何在C#專案中安裝IronPrint以供使用?
您可以通過NuGet使用命令Install-Package IronPrint安裝IronPrint,然後在您的C#文件中包括using IronPrint;以訪問其功能。
IronPrint支持哪些紙張大小?
IronPrint支持多種紙張大小,包括ISO標準如A0、A1、A2、A3、A4、A5,以及常見的美國尺寸如Letter、Legal和Executive。
如何訪問IronPrint的完整API參考資料?
可以通過IronSoftware網站上的文檔訪問IronPrint的完整API參考資料,提供有關類和方法的詳細資訊。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。