如何在 C# 中靜默打印 PDF 文件
PDF文件是一種可攜式文件格式,可以要求的格式儲存和傳輸資料。 它能保持所儲存資料的格式,使用戶能夠專注於處理數位文件的其他方面。 列印電子文檔是一項繁瑣的任務,因為資料在不同的平台上顯示效果不同。 但是,以 PDF 文件形式傳送資料有助於保持列印格式。 然而,對於 C# 開發人員來說,以程式方式列印 PDF 文件可能是一個挑戰。 透過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 或 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 網站下載並安裝。 2.建立專案- 若要使用 Visual Studio 2022 建立一個用於 PDF 列印的 C# 控制台應用程式,請按照以下步驟操作: 開啟 Visual Studio,然後按一下"建立新專案"
<div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-1.webp" alt="Visual Studio" class="img-responsive add-shadow" /> <p class="content__image-caption">Visual Studio</p> </div>選擇 C# 控制台應用程式,然後按一下"下一步"。
<div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-2.webp" alt="新專案對話框" class="img-responsive add-shadow" /> <p class="content__image-caption">新專案對話框</p> </div>現在,輸入您的項目名稱,選擇位置,然後按一下"下一步"。
<div class="content-img-align-center"> <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-3.webp" alt="Web Forms" class="img-responsive add-shadow" /> <p class="content__image-caption">Web Forms</p> </div>-
為您的應用程式選擇最新的 .NET Framework。 我們將使用穩定版本 6.0。
附加資訊
-
點擊創建,控制台專案就創建好了,我們可以開始以程式方式列印 PDF 文件了。 3.安裝 IronPDF - 有 3 種方法可以下載和安裝 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/");
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 the PDF document using the default printer settings.
Pdf.Print()
此方法會將 PDF 傳送到預設印表機進行列印。
進階列印選項
IronPDF 為靜默列印提供了多種進階列印選項。
使用 PdfDocument.GetPrintDocument 方法,並將結果儲存在 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.
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 (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
{
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 方法
PdfDocument 類別提供了 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);
' 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();
}
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 功能整合到應用程式開發流程中。 另外,檢視和編輯PDF文件不需要Adobe Acrobat Reader。
IronPDF 可免費用於個人開發,也可授權用於商業用途。 It provides a free trial license to access and test out the full functionality of the library. 您可以點擊此連結查看更多詳情。
常見問題解答
如何在 C# 中靜默列印 PDF?
您可以使用 IronPDF 的 Print 方法在 C# 中靜默列印 PDF。這使您可以在不打開打印機對話框的情況下列印,確保文檔的格式被保存。
設置 IronPDF 進行靜默 PDF 列印有哪些步驟?
要設置 IronPDF 以進行靜默 PDF 列印,請通過 NuGet 套件管理器安裝庫或下載 .DLL 文件。然後,使用 Print 方法配置靜默列印設置,並指定如 PrinterName 和 PrinterResolution 等打印機屬性。
如何在列印時確保 PDF 格式的維護?
IronPDF 允許您配置特定的打印機設置,如設置 PrinterResolution 屬性,以幫助維持 PDF 格式,確保列印輸出的與原始文檔匹配。
我可以在 C# 中將 PDF 列印到文件位置嗎?
是的,IronPDF 提供 PrintToFile 方法,這允許您直接將 PDF 列印到指定的文件位置而不打開打印機對話框。
不使用 Adobe Acrobat Reader 是否可以列印 PDF?
是的,IronPDF 使您能夠列印、查看和編輯 PDF,而無需 Adobe Acrobat Reader,這使其成為在 C# 應用程式中處理 PDF 文件的靈活工具。
IronPDF 是否支援跨平台列印 PDF?
是的,IronPDF 支援跨平台列印 PDF,提供 Windows、macOS、Android 和 iOS 的相容性,適用於各種開發環境。
IronPDF 可用的試用選項有哪些?
IronPDF 對於個人開發是免費的,並提供試用許可讓您可以在承諾購買商業許可之前測試其全部功能。
如何指定特定打印機來列印 PDF 與 IronPDF?
您可以設置 IronPDF 中的 PrinterSettings.PrinterName 屬性,將打印任務定向到您所需的打印機。
開發與 IronPDF 的 C# 應用程式建議使用哪個 IDE?
Visual Studio 是開發與 IronPDF 的 C# 應用程式的推薦集成開發環境(IDE),提供強大的編碼和項目管理平台。
如何排查 C# 中靜默 PDF 列印的問題?
如果您在 C# 中遇到靜默 PDF 列印問題,請確保 IronPDF 已正確安裝,並且像 PrinterName 和 PrinterResolution 之類的打印機設置已準確配置。查閱 IronPDF 文檔也可以提供有用的故障排除提示。


