跳過到頁腳內容
使用 IRONPRINT

C# 將 PDF 發送到打印機(逐步教程)

PDF 指的是"便攜式文件格式"。 在很多情況下,開發人員需要在應用程式中以程式方式列印 PDF 檔案。 在 C# 中,這可能是一項非常繁瑣的任務,但多虧了IronPDF,只需幾行程式碼即可輕鬆完成。 該工具允許我們使用預設印表機設定以及自訂列印選項來列印 PDF 文件。 在本教程中,您將學習如何使用 C# 語言列印 PDF 檔案。

本教程涵蓋的主題

本文將涵蓋以下主題:

IronPDF庫

  • 建立 C# 控制台項目
  • 安裝IronPDF
    1. NuGet套件管理器
    2. NuGet套件管理器控制台
    3. 使用 DLL 文件
  • 新增IronPDF命名空間
  • 列印 PDF 文件
    1. 建立 PDF 文件並列印 PDF 文件
    2. 根據 URL 建立 PDF 文件並列印
    3. 先進印刷技術
  • 概括

IronPDF

IronPDF是一個適用於.NET Framework的 PDF 庫,它允許開發人員輕鬆建立 PDF 文件。 IronPDF 在 Google Chrome 桌面版上的渲染效果"像素完美"。 IronPDF只需一行程式碼即可輕鬆建立 PDF 文件。 它無需 Acrobat Reader 或其他 PDF 檢視器即可處理 PDF 文件。

IronPDF可用於從 HTML 字串、HTML 檔案或 URL 建立 PDF 檔案。 之後,它可以將這些文件發送到預設印表機進行列印。

A free trial of IronPDF is available.

IronPDF庫的一些重要特性

  • 使用 HTML 4 和 5、CSS 和JavaScript建立 PDF 文檔
  • 根據 URL 產生 PDF 文檔
  • 將 PDF 檔案列印到預設的實體印表機
  • 設定列印作業設定(例如,列印特定頁面等)
  • 載入包含自訂網頁登入憑證、使用者代理程式、代理伺服器、Cookie、HTTP 標頭和表單欄位或變數的 URL,從而允許存取 HTML 登入表單後面的網頁。
  • 讀取並填寫 PDF(便攜式文件格式)表單欄位數據
  • 從 PDF 檔案中提取圖像和文字 對 PDF 文件進行數位簽名 無需第三方函式庫

1. 建立一個 C# 項目

本教學將使用 Visual Studio 2022,但您也可以使用更早的版本。

開啟 Visual Studio 2022。

  • 建立一個新的 C# .NET控制台專案。 選擇.NET Core控制台應用程式。
How to Send PDFs to Printer Using C#, Figure 1: 控制台應用程式

控制台應用程式

  • 為項目命名,例如 DemoApp。 我們將使用.NET Framework 6.0 的最新、最穩定的版本。 點擊"創建"按鈕。
How to Send PDFs to Printer Using C#, Figure 2: .NET Framework

.NET Framework

2. 安裝IronPDF庫

要安裝IronPDF庫,我們可以使用以下任何一種方法:

2.1. NuGet套件管理器

我們可以從NuGet套件管理器安裝IronPDF C# .NET Core庫。

點選"工具"> "NuGet套件管理員">"管理解決方案的NuGet套件",開啟套件管理員。

How to Send PDFs to Printer Using C#, Figure 3: 軟體套件管理器

軟體套件管理器

或者,在解決方案資源管理器中右鍵單擊項目,然後按一下"管理NuGet套件"。

How to Send PDFs to Printer Using C#, Figure 4: NuGet套件管理器 - 解決方案資源管理器

NuGet套件管理器 - 解決方案資源管理器

搜尋IronPDF。 選擇IronPDF並點選安裝。 庫將開始安裝。

How to Send PDFs to Printer Using C#, Figure 5: 安裝IronPDF

安裝IronPDF

2.2. NuGet套件管理器控制台

點選"工具"> "NuGet套件管理器">"套件管理器控制台",開啟NuGet套件管理員控制台。

在命令列中輸入以下命令:

Install-Package IronPrint
How to Send PDFs to PRinter Using C#, Figure 6: NuGet 軟體包管理器控制台

軟體包管理器控制台

2.3. 使用 DLL 文件

在專案中使用IronPDF 的另一種方法是新增IronPDF庫中的 DLL 檔案。 你可以從這個鏈接下載DLL檔。

  • 下載 DLL 壓縮檔。將其解壓縮到指定資料夾。
  • 在 Visual Studio 中開啟一個專案。 在解決方案資源管理器中,右鍵單擊"引用",然後瀏覽並找到IronPDF DLL 檔案。

2.4. 新增IronPDF命名空間

安裝完成後,將IronPDF和 System.Drawing 命名空間加入您的程式檔案。

using IronPdf;
using System.Drawing;
using IronPdf;
using System.Drawing;
$vbLabelText   $csharpLabel

注意:您必須將這些引用新增至您希望使用 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();
$vbLabelText   $csharpLabel

這段程式碼將建立一個 PDF 文件,其中包含 RenderHtmlAsPdf 函數中傳遞的 HTML 內容。 此函數將 HTML 片段轉換為 PDF 文件。

若要使用IronPDF庫產生 PDF 檔案或 PDF 頁面,您必須熟悉 HTML 標籤。 我們使用 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();
$vbLabelText   $csharpLabel

這裡,根據指定的 URL 建立 PDF 文件,然後將其列印出來。

How to Send PDFs to PRinter Using C#, Figure 7: 列印從 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();
}
$vbLabelText   $csharpLabel

在上面的程式碼範例中,選擇了"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
};
$vbLabelText   $csharpLabel

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();
}
$vbLabelText   $csharpLabel

5. 總結

IronPDF是一個用於處理 PDF 文件的完整解決方案。 它能夠將不同格式的文件轉換為 PDF 格式。 借助IronPDF庫函數,PDF 文件的操作和格式化變得非常容易。 只需幾行程式碼即可建立和格式化 PDF 文件。它還可以透過程式列印 PDF 文件。 它透過將 PDF 文件發送到電腦的預設印表機來實現這一點。 我們既可以向使用者顯示列印對話框窗口,也可以使用 Print 方法的重載進行靜默列印。

A free trial of IronPDF is also available to test its full potential to generate and print PDF documents in your applications. 有關許可的更多信息,請點擊此鏈接

此外,目前的特別優惠活動讓您只需支付兩件 Iron Software 產品的價格,即可獲得五件Iron Software產品。

常見問題解答

我如何能夠在 C# 中編程打印 PDF 文件?

您可以使用 IronPDF 在 C# 中編程打印 PDF 文件。通過使用 Print 等方法,您可以將 PDF 發送到默認或指定的打印機並自定義打印選項。

安裝 C# 中的 PDF 庫以進行打印的步驟有哪些?

要安裝 IronPDF 庫,您可以使用 Visual Studio 中的 NuGet 包管理器、NuGet 包管理器控制台,或通過 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,使其在不同的開發環境中具有多樣性。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我