
如何在 C# 中列印 PDF 文件不顯示對話框
IronPrint 是 Iron Software 全新的 .NET 列印程式庫,可相容於各種平台,包括 Windows、macOS、Android 和 iOS。立即開始使用 IronPrint!
這篇文章探討了直接列印PDF的重要性,並演示了如何使用強大的C#程式庫IronPDF來簡化這個過程。
IronPDF - .NET Framework PDF程式庫
IronPDF是一個強大的C#程式庫,專為簡化PDF文件的建立、操作和交互而設計。 使用IronPDF,開發者可以輕鬆地從HTML內容、圖像及其他來源生成PDF,這使其成為簡單和複雜PDF相關任務的多功能工具。 其功能不僅限於簡單的PDF文件生成; IronPDF還使您能夠合併、拆分並以多種方式操作PDF。 此外,其直觀的API使新手和有經驗的開發者都可以輕鬆上手。 一些重要功能包括:
- 從HTML4/5、CSS、JavaScript和圖片建立PDF文件。
- 從URL生成PDF文件。
- 使用自訂網路登入憑證載入URL。
- 加密和解密PDF。
- 合併現有的PDF文件。
- 建立和編輯PDF表單。
直接列印PDF的重要性
在處理PDF時,列印是一項基本需求。 不透過列印對話框直接從C#應用程式列印PDF提供了多種優勢。 這樣做可通過消除不必要的交互來增強使用者體驗,自動化列印任務,並實現與更大工作流程的無縫整合。 IronPDF簡化了這一過程,讓開發者能夠在保持對列印過程的控制的同時,確保紙本輸出的完整性。
先決條件
在進入PDF列印過程之前,請確保您具備以下先決條件:
- **Visual Studio:**這是一個整合的開發環境(IDE),您可以在其中建立、編輯和編譯您的C#專案。 要在您的電腦上下載並安裝,請存取官方Visual Studio網站。
- IronPDF:IronPDF程式庫,可以輕鬆使用NuGet包管理器整合到您的專案中。
建立Visual Studio專案
建立Visual Studio控制台專案是一個簡單的過程。 按照以下步驟使用Visual Studio建立新控制台應用程式:
-
**開啟Visual Studio:**啟動您的Visual Studio IDE。
-
**建立新專案:**Visual Studio開啟後,點擊"建立新專案"。
-
**選擇專案模板:**在"建立新專案"窗口中,您將看到專案模板的列表。 選擇Visual C#控制台應用程式。
在Visual Studio中建立新的C#控制台應用程式 -
**設定專案詳細資訊:**選擇模板後,系統將提示您設定專案詳細資訊。
設定您的新專案 -
**設定其他設定:**選擇具有長期支援的.NET Framework。 IronPDF支援.NET Framework的最新版本。
-
建立專案:配置專案詳情後,點擊建立按鈕。 Visual Studio會建立專案並在IDE中打開。
透過NuGet安裝IronPDF
您可以參考以下步驟在您的專案中安裝IronPDF:
-
開啟Visual Studio和您的專案。
-
前往"工具"選單並選擇"NuGet包管理器",然後點選"管理解決方案的NuGet包"。
導航至NuGet包管理器 -
在"瀏覽"標籤中,在搜尋框中搜尋"IronPDF"。
在NuGet包管理器使用者介面中搜索IronPDF -
點選
IronPdf包並為您的專案選擇它,然後點擊"安裝"按鈕。
IronPDF無對話框列印 - 步驟過程
導入IronPdf命名空間
程式碼開始時先導入System.Drawing.Printing命名空間,這使得可以使用IronPDF程式庫中的類以及System命名空間中的繪製和列印選項。
using IronPdf;
using System.Drawing.Printing;Imports IronPdf
Imports System.Drawing.Printing使用ChromePdfRenderer建立PDF文件
ChromePdfRenderer負責渲染網頁。 PdfDocument類表示PDF文件,並提供方法執行各種操作,包括列印。 以下程式碼從IronPDF網站URL(https://ironpdf.com/)中生成PDF文件:
// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");' Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
Dim renderer As New ChromePdfRenderer()
' Create a PdfDocument by rendering a specified URL as a PDF
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")在上述程式碼範例中,第一行初始化一個ChromePdfRenderer實例,來自IronPDF程式庫,負責將網頁轉換為PDF格式。 第二行使用此實例從指定URL渲染內容以建立PdfDocument,啟用進一步的PDF相關操作。
無對話框列印PDF文件
// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");' Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")上述程式碼行使用指定的列印解析度(DPI)啟動PdfDocument的列印過程,將其發送到"Microsoft Print to PDF"列印機,這通常是預設列印機(如果沒有安裝列印機),並將列印輸出保存為名為"printedfile1.pdf"的PDF文件在文件目錄中。 您可以根據需要更改列印機名稱和文件位置。
PDF文件是以像素完美的方式列印出來的:
名為"printedfile1.pdf"的PDF文件輸出圖片
帶有高級列印選項的靜默列印
要更好地控制程式化列印PDF文件,請查看以下帶有高級選項的程式碼片段:
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Set the printer name to "Microsoft Print to PDF"
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Specify the file name for the printed document
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
// Enable printing to file
printDocument.PrinterSettings.PrintToFile = true;
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Initiate the print process
printDocument.Print();
}Using printDocument = pdfDocument.GetPrintDocument()
' Set the printer name to "Microsoft Print to PDF"
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Specify the file name for the printed document
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
' Enable printing to file
printDocument.PrinterSettings.PrintToFile = True
' Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Initiate the print process
printDocument.Print()
End Using在上述程式碼片段中,IronPDF促進了高級PDF列印自訂。 它從PrintDocument,允許自訂列印機設置、列印機名稱、輸出文件名和解析度。 設定PrintFileName很重要,因為它可讓您繞過列印對話框直接列印。 然後程式碼觸發列印,使用列印方法將PDF文件內容導向到指定的列印機,例如,本例中的是"Microsoft Print to PDF"。 最後將輸出保存為PDF文件。這使這無縫、無需對話框的PDF列印具有自定義設置。
您還可以指定頁面範圍以便無需列印機對話框和Adobe Acrobat Reader的情況下列印PDF文件,只需將IronPDF整合到您的專案中。 有關列印的更多詳細資訊,請存取程式碼範例頁。
輸出
執行專案後,兩個輸出列印的PDF文件在指定的資料夾裡生成,無需使用者參與。 您還可以比較使用高級選項後的尺寸差異。
顯示列印的兩個PDF文件's "printedfile1.pdf" 和 "printedfile2.pdf" 的圖片
結論
直接從C#應用程式列印PDF文件簡化了文件處理並增強了使用者體驗。 IronPDF及其一系列PDF操作工具,使開發者能夠無縫列印PDF文件。 透過將IronPDF整合到您的C#項目中,您可以充分利用其功能。
IronPDF是一個以其強大PDF處理能力著稱的商業程式庫。 它提供免費試用期,允許使用者測試和體驗其功能。 在試用期結束後,您可以獲取授權以持續使用。 若要開始,您可以從官方IronPDF網站下載產品。

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

