使用 IRONPRINT

C# 將 PDF 發送到打印機(步驟指南)

發佈 2022年12月6日
分享:

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 框架的 PDF 庫,允許開發人員輕鬆創建 PDF 文件。IronPDF 的渲染對於桌面版 Google Chrome 幾乎完美。IronPDF 能夠使用單行代碼輕鬆創建 PDF 文檔。它可以在沒有 Acrobat Reader 或其他 PDF 查看器的情況下處理 PDF 文件。

IronPDF 可以從 HTML 字串、HTML 檔案或網址生成 PDF 文件。之後,它可以將這些文件發送到默認的打印機進行打印。 免費試用 IronPDF 現已推出。

IronPDF 库的一些重要功能

  • 从 HTML 4 和 5、CSS 和 JavaScript 创建 PDF 文档
  • 从 URLs 生成 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 是我們將使用的最新和最穩定版本。點擊 "Create" 按鈕。
How to Send PDFs to Printer Using C#, Figure 2: .NET框架

.NET框架

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.DLL 命名空間添加到您的程式文件。

using IronPdf;
using System.Drawing.dll;
using IronPdf;
using System.Drawing.dll;
Imports IronPdf
Imports System.Drawing.dll
VB   C#

注意:您必須在每個希望使用IronPDF功能的檔案中添加這些引用。

IronPDF 已安裝並準備就緒! 我們現在可以為我們的 .NET Core 應用程式創建第一個 PDF 文件並將其發送到默認打印機進行打印。讓我們通過代碼範例來查看其中的一些例子。

3. 列印 PDF 文件

3.1. 從 HTML 創建和列印 PDF 文件

將 HTML 字串轉換為 PDF 格式非常容易。這個新創建的文件可以使用 IronPDF 來列印。以下是簡單創建 PDF 的代碼。

// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
' Render any HTML fragment or document to HTML
Dim html= New ChromePdfRenderer()
Dim PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file")

' Send the PDF to the default printer to print
Pdf.Print()

Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
VB   C#

此代码将使用传递给 RenderHtmlAsPdf 函数的 HTML 内容创建一个 PDF 文件。此函数将 HTML 片段转换为 PDF 文档。

您必须熟悉 HTML 标签才能使用 IronPDF 库生成 PDF 文件或 PDF 页面。我们使用 Print 函数将 PDF 文件的输出发送到打印机。打印机对话框将出现,允许您确认打印任务。

3.2. 從URL創建和列印PDF文件

您也可以使用URL創建PDF文件:

var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
Dim Render = New ChromePdfRenderer()
Dim PDF = Render.RenderUrlAsPdf("https://ironpdf.com/")

' Send the PDF to the default printer to print
Pdf.Print()

Dim PrintDoc As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
VB   C#

PDF 將會如下所示列印:

如何使用 C# 將 PDF 發送到打印機,圖 7:打印從 URL 生成的 PDF

套件管理控制台

4. 進階列印選項

IronPDF 功能多樣,非常擅長處理列印功能,例如尋找印表機和設定列印解析度。

4.1 指定打印機

要指定打印機,您只需要獲取當前的打印文件物件。 (在 GetPrintDocument 方法的幫助下)然後使用PrinterSettings.PrinterName屬性。您可以選擇任何可用的打印機。

using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.Print()
End Using
VB   C#

在以上的程式碼範例中,我選擇了 "Microsoft Print to PDF"。有關更多資訊 設定特定的打印設定 可以在文件頁面中找到。

4.2 設定打印解析度

您還可以設定列印 PDF 的解析度。解析度是指列印或顯示的像素數,取決於您的輸出。您還可以通過 IronPDF 設定列印文件的解析度,使用 DefaultPageSettings.PrinterResolution PDF文件的屬性。

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}
VB   C#

4.3 使用C#追踪打印过程

在以下程式碼範例中,您將看到如何更改打印機名稱、解析度以及如何获取已打印頁數的计数。

using (var printDocument = PDF.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };
    var printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;
    printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };
    var printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;
    printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}
	Dim 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
	printDocument.Print()
End Using
VB   C#

5. 總結

IronPDF 是一個完整的 PDF 文檔處理解決方案。它提供了從不同格式轉換為 PDF 的能力。使用 IronPDF 函式庫,PDF 文件的操作和格式化變得非常簡單。只需要幾行代碼就可以創建和格式化 PDF 文件。它還能程式化地列印 PDF。它通過將 PDF 發送到計算機的默認打印機來列印。我們可以向用戶顯示列印對話框窗口,或者使用 Print 方法的重載靜默列印。

A 免費試用 IronPDF 的完整版也可供您測試其在生成和打印 PDF 文件方面的全部潛力。關於許可證的更多資訊可於此查看 連結此外, 當前特別優惠 讓您以兩個價格獲得五個Iron Software產品。

< 上一頁
C#以程式方式列印PDF(程式碼範例教學)
下一個 >
如何在VB.NET中打印PDF(逐步)教程

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 9,531 查看許可證 >