C# 將 PDF 發送到列印機(逐步教程)
PDF 意味著"可攜式文件格式"。 在許多情況下,開發者需要在應用程式中以程式方式列印 PDF 檔案。 在 C# 中,這可能是一個非常繁瑣的任務,但感謝 IronPDF,只需幾行程式碼就可以輕鬆完成。 這個工具允許我們使用預設印表機設定和自訂列印選項來列印 PDF 文件。 在本教程中,您將學習如何使用 C# 語言列印 PDF。
本教程涵蓋的主題
這裡將涵蓋以下主題:
- IronPDF 程式庫
- 建立一個 C# 控制台專案
- 安裝 IronPDF
- NuGet 套件管理器
- NuGet 套件管理器控制台
- 使用 DLL 檔案
- 新增 IronPDF 命名空間
- 列印 PDF 文件
- 建立 PDF 文件並列印它們
- 從 URL 建立 PDF 文件並列印
- 進階列印
- 總結
如何在 C# 中將 PDF 發送到印表機
- 安裝 C# 程式庫以將 PDF 發送到列印機
- 利用
Print方法以預設印表機設定列印 PDF - 透過設置
PrinterName屬性發送到特定印表機 - 設置
PrinterResolution屬性來自訂印表機的解析度 - 在 C# 中追蹤印刷頁數
IronPDF
IronPDF 是 .NET Framework 的 PDF 程式庫,可以讓開發人員輕鬆建立 PDF 文件。 IronPDF 的渲染對於 Google Chrome 桌面版本是"像素完美的"。 IronPDF 可以通過一行程式碼輕鬆建立 PDF 文件。 它可以在沒有 Acrobat Reader 或其他 PDF 閱讀器的情況下處理 PDF 文件。
IronPDF 可用於從 HTML 字串、HTML 檔或 URL 建立 PDF 檔案。 然後,它可以將這些檔案發送到預設的印表機以進行列印。
IronPDF 的免費試用版可用。
IronPDF 程式庫的一些重要功能
- 從 HTML 4 和 5、CSS 和 JavaScript 建立 PDF 文件
- 從 URL 生成 PDF 文件
- 將 PDF 列印到預設的實體印表機
- 設定列印作業設定(例如列印特定頁面)
- 使用自訂網路登入憑證取得 URL,使用者代理,代理,Cookie,HTTP 標頭及表單欄位或變數,從而允許存取 HTML 登入表單後的網頁
- 讀取和填寫 PDF(可攜式文件格式)表單字段資料
- 從 PDF 文件中提取圖像和文字
- 用電子方式簽署 PDF 文件
- 無需第三方程式庫
1. 建立一個 C# 專案
本教程將使用 Visual Studio 2022,但您也可以使用早期版本。
- 打開 Visual Studio 2022。
- 建立一個新的 C# .NET 控制台專案。 選擇 .NET Core 控制台應用程式。
控制台應用程式
- 給專案取一個名稱,例如 DemoApp。
- .NET Framework 6.0 是我們將使用的最新最穩定的版本。 點擊"建立"按鈕。
.NET Framework
2. 安裝 IronPDF 程式庫
要安裝 IronPDF 程式庫,我們可以使用以下列出的方法之一:
2.1. NuGet 套件管理器
我們可以從 NuGet 套件管理器安裝 IronPDF C# .NET Core 程式庫。
通過點擊 工具 > NuGet 套件管理器 > 管理解決方案的 NuGet 套件 打開套件管理器。
套件管理器
或者,在 方案總管 中右擊專案,然後點擊 管理 NuGet 套件。
NuGet 套件管理器 - 方案總管
搜尋 IronPDF。 選擇 IronPDF,然後單擊安裝。 程式庫開始安裝。
安裝 IronPDF
2.2. NuGet 套件管理器控制台
點擊 工具 > NuGet 套件管理器 > 套件管理器控制台。 打開 NuGet 套件管理器控制台。
在命令行中輸入以下命令:
Install-Package IronPrint
套件管理器控制台
2.3. 使用 DLL 檔案
在專案中使用 IronPDF 的另一種方法是從 IronPDF 程式庫新增 DLL 檔案。 您可以從這個 連結 下載 DLL 檔案。
- 下載 DLL zip 檔。將其提取到特定文件夾。
- 在 Visual Studio 中打開一個專案。 在方案總管中右擊"引用"並瀏覽 IronPDF DLL 檔案。
2.4. 新增 IronPDF 命名空間
安裝完成後,將 IronPDF 和 System.Drawing 命名空間新增到您的程式文件中。
using IronPdf;
using System.Drawing;
using IronPdf;
using System.Drawing;
Imports IronPdf
Imports System.Drawing
注意:您必須將這些引用新增到您希望使用 IronPDF 功能的每個文件中。
IronPDF 已安裝並準備就緒! 現在我們可以為我們的 .NET Core 應用程式建立第一個 PDF 文件並將其發送到預設印表機列印。 讓我們通過程式碼範例來看看其中的一些。
3. 列印 PDF 文件
3.1. 從 HTML 建立和列印 PDF 文件
處理 HTML 字串並將其轉換為 PDF 格式非常容易。 然後可以使用 IronPDF 列印這個新建立的檔案。 這是輕鬆建立 PDF 的程式碼。
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();
// Render any HTML fragment to a PDF document
using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>");
// Send the PDF to the default printer
pdfDocument.Print();
// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();
// Render any HTML fragment to a PDF document
using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>");
// Send the PDF to the default printer
pdfDocument.Print();
// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
' Create an instance of ChromePdfRenderer
Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer()
' Render any HTML fragment to a PDF document
Dim pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>")
' Send the PDF to the default printer
pdfDocument.Print()
' Create a PrintDocument object that can be used for further configurations
Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
這段程式碼將建立一個包含傳入的 HTML 內容的 PDF 文件 RenderHtmlAsPdf 函式。 此功能執行 HTML 片段到 PDF 文件的轉換。
您必須熟悉 HTML 標籤以使用 IronPDF 程式庫生成 PDF 文件或 PDF 頁面。 我們使用 Print 函式將 PDF 文件的輸出發送到印表機。 印表機對話框將會出現,允許您確認列印工作。
3.2. 從 URL 建立和列印 PDF 文件
您也可以使用 URL 建立 PDF 文件:
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();
// Render a PDF from a URL
var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/");
// Send the PDF to the default printer
pdfDocument.Print();
// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();
// Render a PDF from a URL
var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/");
// Send the PDF to the default printer
pdfDocument.Print();
// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
' Create an instance of ChromePdfRenderer
Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer()
' Render a PDF from a URL
Dim pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/")
' Send the PDF to the default printer
pdfDocument.Print()
' Create a PrintDocument object that can be used for further configurations
Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
在這裡,從指定的 URL 建立 PDF 文件,然後列印。
列印從 URL 生成的 PDF
4. 進階列印選項
IronPDF 非常多才多藝,並且非常擅長處理列印功能,如查找印表機和設置列印解析度。
4.1 指定印表機
要指定印表機,所需的所有操作是獲取當前列印文件物件(使用 GetPrintDocument 方法),然後使用 PrinterSettings.PrinterName 屬性。 您可以選擇任何可用的印表機。
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the document
printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the document
printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Print the document
printDocument.Print()
End Using
在上述程式碼範例中,"Microsoft Print to PDF"已作為印表機選擇。 有關設定特定列印設置的更多資訊可以在文件頁面找到。
4.2 設定印表機解析度
您還可以設定列印 PDF 的解析度。 解析度是指列印的質量即像素數量。 您可以使用 PDF 文件的 DefaultPageSettings.PrinterResolution 屬性設定列印文件的解析度。
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
' Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With {
.Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
4.3 Tracing Printing Processes Using C
在下面的程式碼範例中,您將看到如何更改印表機名稱、設定解析度以及獲取已列印頁面的數量。
int printedPages;
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Track number of printed pages
printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;
// Print the document
printDocument.Print();
}
int printedPages;
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Track number of printed pages
printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;
// Print the document
printDocument.Print();
}
Dim printedPages As Integer
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With {
.Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Track number of printed pages
printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: printDocument.PrintPage += (sender, args) => printedPages++;
AddHandler printDocument.PrintPage, Sub(sender, args) printedPages
printedPages += 1
' Print the document
printDocument.Print()
End Using
5. 總結
IronPDF 是一個完整的解決方案,用於處理 PDF 文件。 它提供了從不同格式轉換為 PDF 的能力。 使用 IronPDF 程式庫函式進行 PDF 文件的操作和格式化變得非常簡單。 只需要幾行程式碼即可建立和格式化 PDF 文件。它還可以程式化地列印 PDF。 它通過將 PDF 發送到電腦的預設印表機來做到這一點。 我們可以選擇向使用者顯示列印對話窗口,或者我們可以使用 Print 方法的重載來靜默列印。
IronPDF 的免費試用版也可用於測試其在您的應用程式中生成和列印 PDF 文件的全部潛力。 有關授權的更多資訊可以在此連結中找到。
此外,當前特別優惠允許您以僅兩個的價格獲得五個 Iron Software 產品。
常見問題
如何在 C# 中以編程方式列印 PDF 文件?
您可以使用 IronPDF 在 C# 中以編程方式列印 PDF 文件。通過使用如 Print 等方法,您可以將 PDF 發送到預設或指定的列印機,並提供可自訂的列印選項。
安裝 PDF 程式庫以進行 C# 列印有哪些步驟?
要安裝 IronPDF 程式庫,您可以使用 Visual Studio 的 NuGet Package Manager、NuGet Package Manager Console,或通過 DLL 檔案新增程式庫。
我可以使用此程式庫從 HTML 建立 PDF 嗎?
是的,IronPDF 允許您使用 RenderHtmlAsPdf 方法從 HTML 建立 PDF。這使得 HTML、CSS 和 JavaScript 可以轉換為 PDF 文件。
如何使用此程式庫將 PDF 發送到特定的列印機?
要將 PDF 發送到特定的列印機,您可以在 IronPDF 的 PrintDocument 物件中設置 PrinterSettings.PrinterName 屬性為所需的列印機名稱。
此程式庫支援哪些高級列印選項?
IronPDF 支援高級列印選項,如指定列印機設置、調整列印解析度,以及追踪列印頁數。
是否可以使用此 PDF 程式庫設置自訂列印解析度?
是的,您可以在 IronPDF 中使用 PrintDocument 物件的 DefaultPageSettings.PrinterResolution 屬性設置自訂的列印解析度。
如何使用此程式庫將 URL 呈現為 PDF?
您可以使用 IronPDF 的 RenderUrlAsPdf 方法將 URL 呈現為 PDF,這使您可以將網頁轉換為 PDF 文件。
此程式庫是否提供免費試用?
是的,IronPDF 提供免費試用,讓使用者探索其在生成和列印 PDF 文件方面的功能。
我可以使用此程式庫追踪已列印的頁數嗎?
是的,IronPDF 允許您在列印作業過程中存取 PrintDocument 物件的屬性來追踪已列印的頁數。
此 PDF 列印程式庫相容哪些平台?
IronPDF 與多個平台相容,包括 Windows、macOS、Android 和 iOS,使其適合不同的開發環境。


