
如何在 C# 中靜默列印 PDF 文件
IronPrint 是 Iron Software 全新的 .NET 列印程式庫,可相容於各種平台,包括 Windows、macOS、Android 和 iOS。立即開始使用 IronPrint!
PDF文件是一種可攜帶文件格式,可按請求的格式儲存和傳輸資料。 它保留了儲存資料的格式,允許使用者專注於處理數位文件的其他方面。 列印數位文件是一項繁瑣的工作,因為資料在不同平台上顯示不同。 但是,將資料發送到PDF文件有助於保持格式以便列印。 然而,以程式方式列印PDF文件可能對於C#的開發者來說是個挑戰。 感謝**IronPDF - C# PDF程式庫,**它使列印PDF文件的過程極其簡單且無煩惱。
本文將解釋如何使用IronPDF程式庫在C#中靜默列印PDF文件。
如何在C#中靜默列印PDF文件
- 安裝C#程式庫以靜默列印PDF文件
- 使用
Print方法以預設設定列印PDF文件 - 設置
PrinterName屬性以針對特定列印機 - 在C#中使用
PrinterResolution類設置自定義解析度 - 將PDF靜默列印到指定目錄,而不打開列印機對話框
IronPDF - C# PDF程式庫
IronPDF是一個C# .NET程式庫,允許開發人員建立、閱讀和編輯PDF文件。 它是一個一流的C#程式庫,優先考慮準確性、易用性和速度。 它專為C#, F#,& VB.NET設計,與**.NET 7, 6, 5, Core, Standard, or Framework.**高度相容。它利用IronPDF強大的Chromium引擎,幫助從HTML生成Web、桌面和控制台的PDF文件。
此外,IronPDF允許使用者輕鬆操作和編輯PDF、新增頁眉和頁腳、從PDF中提取文字和圖像。
一些重要功能包括:
- 從不同文件格式載入並生成PDF文件
- 使用預設列印機保存和列印PDF文件
- 合併和拆分PDF文件
- 無需Adobe Reader的PDF編輯功能
使用IronPDF程式庫在C#中靜默列印PDF文件的步驟
要靜默列印PDF文件,首先我們需要在本地電腦上安裝以下組件。
- Visual Studio - 它是C#開發的官方IDE,必須安裝在電腦上。 您可以從Visual Studio網站下載並安裝。
- 建立專案 - 要在C#中建立一個用於PDF列印的控制台應用程式,請按照以下步驟使用Visual Studio 2022:
-
打開Visual Studio並點擊建立新專案

Visual Studio
-
選擇C#控制台應用程式並點擊下一步

新專案對話框
-
現在,輸入項目名稱,選擇位置,然後點擊下一步

Web Forms
-
選擇最新的.NET框架為您的應用程式使用。 我們將使用穩定版6.0。

額外資訊
-
點擊建立,控制台專案已建立,我們準備以程式方式列印PDF文件。
-
- 安裝IronPDF - 有三種方式下載和安裝IronPDF程式庫。 如下所示:
-
使用Visual Studio - Visual Studio具有NuGet包管理器,幫助在C#專案中安裝NuGet包。
-
點擊選單欄上的工具,或
-
右鍵單擊方案資源管理器中的專案文件

方案資源管理器

專案選單>管理NuGet封裝
-
一旦打開,在NuGet包管理器中瀏覽IronPDF並安裝,如下所示:

從NuGet包安裝IronPDF
-
-
直接下載NuGet封裝 - 另一種下載IronPDF的方法是導航到NuGet網站並直接下載包。 這裡是連結https://www.nuget.org/packages/IronPdf/。
-
下載IronPDF .DLL程式庫 - IronPDF也可以直接從IronPDF網站下載。導航至:IronPDF DLL下載以安裝。 在您的專案中引用.DLL文件以使用它。
-
生成PDF文件和列印任務
在這裡,我們將從URL生成一個PDF文件。 建立PDF文件很簡單,通常是兩步過程。 以下程式碼範例生成了一個PDF:
using IronPdf;
// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");Imports IronPdf
' Initialize a new instance of ChromePdfRenderer.
Private Renderer As New ChromePdfRenderer()
' Render the specified URL as a PDF document.
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")使用上述程式碼建立的PDF文件物件已準備好列印。 接下來,我們將使用預設列印機紙張列印PDF文件。 這段程式碼是一行,內容如下:
// Print the PDF document using the default printer settings.
Pdf.Print();' Print the PDF document using the default printer settings.
Pdf.Print()這個Print方法將PDF發送到預設列印機進行列印。
高級列印選項
對於靜默列印,IronPDF提供各種高級列印選項。
使用System.Drawing.Printing.PrintDocument物件中。 程式碼簡單,內容如下:
// Remember to add assembly reference to System.Drawing.dll in project
// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();' Remember to add assembly reference to System.Drawing.dll in project
' Get the print document for the PDF.
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()指定列印機名稱
IronPDF還提供列印到特定列印機的機會。 要指定名稱,使用PrinterSettings.PrinterName屬性。 首先,我們需要獲取當前的PDF文件物件。 程式碼範例如下:
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Send the print job to the specified printer.
printDocument.Print();
}Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Send the print job to the specified printer.
printDocument.Print()
End Using設置列印機解析度
另一個很酷的功能是設置列印機解析度。 我們可以根據輸出控制要列印的像素數量。 來自PDF文件的DefaultPageSettings.PrinterResolution屬性可用於設置解析度。 這裡是一個非常簡單的程式碼範例:
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Send the print job with the custom resolution settings.
printDocument.Print();' Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Send the print job with the custom resolution settings.
printDocument.Print()PrintToFile方法
這個PrintToFile方法,允許我們在C#中將PDF列印到文件。 它將pathToFile作為參數,用來直接列印文件到該位置而不打開列印機對話框。 程式碼簡單,內容如下:
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);' Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", False)完整的程式碼範例如下:
using IronPdf;
// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Execute the print job.
printDocument.Print();
}Imports IronPdf
' Initialize the PDF renderer and create the PDF document.
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Execute the print job.
printDocument.Print()
End Using執行程式碼後,它將URL轉換為PDF文件。 然後要靜默列印PDF文件,使用GetPrintDocument方法。 成功編譯和執行程式文件後,出現列印對話框,將其保存為PDF文件。 然後使用指定的列印機名稱保存該PDF。


總結
在本文中,我們仔細研究了如何使用IronPDF靜默列印PDF文件。 IronPDF在PDF列印時提供了許多有用的選項。 它還可以跟蹤列印的頁面,並允許在頁面範圍內列印。
靜默列印及其他列印選項,使IronPDF成為C#中處理PDF時一個傑出的程式庫。
IronPDF有助於將不同格式的資料轉換為PDF,從PDF轉為不同格式。 它使開發者更容易在應用程式開發過程中整合PDF功能。 更重要的是,它不需要Adobe Acrobat Reader來查看和編輯PDF文件。
IronPDF對於個人開發免費,並可用於商業用途授權。 它提供一個免費試用授權以存取並測試程式庫的全部功能。 您可以在此連結查詢更多細節。

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

