跳過到頁腳內容
使用 IRONPRINT

使用 IronPDF 在 C# 中打印 PDF 文件

PDF是最常用的文件格式。 它體積小巧,並且受到大多數作業系統的支援。 PDF檔案可以輕鬆地在不同裝置之間共用。 它們在任何介面和作業系統上看起來都完全一樣,因此閱讀起來非常方便。 使用者可以透過包括手機、平板電腦等在內的所有裝置存取 PDF 檔案。

由於其受歡迎程度,所有 C# 開發人員都必須知道如何使用 C# 列印 PDF 文件。 這是因為當您需要列印發票、薪資單、訂單或個人資料等文件時,可能會遇到問題。這些文件通常是 PDF 文件格式,因此我們必須知道如何使用 C# 列印 PDF 文件。

在 C# 中,可以使用多個庫來列印 PDF 文件。 但是,您可能會遇到的一個問題是許可證費用。 它們大多數都不是免費的,或者使用起來很困難。 但幸運的是,你不必擔心。 我找到了在 C# 中列印 PDF 文件的最簡單方法。

I will use the IronPDF Library to print PDF documents. 相信我,你一定會喜歡的!


你從這篇文章中能獲得什麼?

閱讀本文後,您將能夠完成以下任務:

  • 使用 C# 列印單一 PDF 文檔
  • 使用 C# 一次列印多個 PDF 文件
  • 將網址轉換為PDF格式,然後列印出來。

為了更好地理解本教程,您必須具備 C# 程式設計的基礎知識。

在開始教學之前,我們先來了解IronPDF是什麼。


IronPDF:。

IronPDF 是一個 .NET 函式庫,它為我們提供了一個最簡單的方法來讀取、建立、操作和列印 PDF 檔案。 該程式庫可用於 C# 和 VB .NET 應用程式。 它能輕鬆與 ASP.NET、ASP.NET core、MVC、Web Forms 以及 .NET 和 .NET core 上的 Web API 搭配使用。 它允許用戶下載PDF文件,透過電子郵件發送,並將其儲存在雲端。

您可以透過此連結了解更多關於 IronPDF 的資訊。

讓我們開始示範。

開啟 Visual Studio

開啟 Visual Studio。 如果您要將此功能新增至現有軟體中,請建立新專案或開啟現有專案。

本教學使用控制台應用程式; 您可以根據軟體需求選擇任一方案。

Project Created

項目已建立。

安裝 NuGet 套件

Open the console by clicking on Tools > NuGet Package Manager > Package Manager 命令列 from the Menu bar on the top of the window.

Csharp Print Pdf Files 2 related to 安裝 NuGet 套件

NuGet 套件管理器控制台將會打開,如下所示:

NuGet Package Manager 命令列

NuGet Package Manager 命令列

Write the following command in the console to Install IronPdf NuGet Package and press Enter.

Install-Package IronPrint

NuGet 套件將會如下所示進行安裝:

安裝 NuGet 套件

安裝 NuGet 套件


用 C# 列印 PDF 文件

現在,讓我們來看一個例子,示範如何使用 IronPDF 庫在 C# 中列印 PDF 文件。

開啟Program.cs檔案。在Program.cs檔案頂部新增以下命名空間以使用 IronPDF:

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

現在,在Main函數中編寫以下程式碼

using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Load the PDF document from file
        using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");

        // Print the loaded PDF document to the default printer
        pdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
        命令列.ReadKey();
    }
}
using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Load the PDF document from file
        using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");

        // Print the loaded PDF document to the default printer
        pdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
        命令列.ReadKey();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

讓我們來理解這段程式碼。

以下程式碼行會將 PDF 檔案載入到記憶體中。 "Sample.Pdf"是檔名,D盤是我的檔案位置。 您必須輸入完整的檔案路徑以及檔案名稱。

using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
End Using
$vbLabelText   $csharpLabel

Print()函數將使用您的預設印表機列印載入的檔案。

測試程式

按 Ctrl + F5 運行程式。

命令列

命令列


用 C# 列印多個 PDF 文檔

我將向您展示如何使用 IronPDF 在 C# 中列印多個 PDF 文件。

14 個 PDF 文件

14 個 PDF 文件

在這個例子中,我需要列印 14 個 PDF 文件。 我已將它們命名為 1 - 14。您可以將所有檔案儲存為 ID,也可以將檔案位置儲存在資料庫中。 從資料庫中提取檔案路徑和檔案名,並將它們儲存到數組或清單中。遍歷該數組或列表,列印出所有文件。

Main函數內部寫入以下程式碼:

using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Loop through each file number and print
        for (int i = 1; i <= 14; i++)
        {
            // Load each PDF document from file
            using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");

            // Inform which file is being printed
            命令列.WriteLine("Printing File: {0}.pdf", i);

            // Print the loaded PDF document to the default printer
            pdfDocument.Print();

            // Print operation success message to console
            命令列.WriteLine("{0}.pdf Printed Successfully!", i);
        }

        命令列.ReadKey();
    }
}
using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Loop through each file number and print
        for (int i = 1; i <= 14; i++)
        {
            // Load each PDF document from file
            using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");

            // Inform which file is being printed
            命令列.WriteLine("Printing File: {0}.pdf", i);

            // Print the loaded PDF document to the default printer
            pdfDocument.Print();

            // Print operation success message to console
            命令列.WriteLine("{0}.pdf Printed Successfully!", i);
        }

        命令列.ReadKey();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

我使用 for 迴圈根據檔案名稱產生 1 到 14 的數字。

運行解決方案:

按 Ctrl + F5 運行程式。

命令列

命令列

將URL轉換為PDF並列印:

我將向您展示如何透過解析 URL 並使用 IronPDF 來列印網頁。 圖書館將透過解析 HTML 來建立 PDF 文件。 我們可以使用列印功能列印該PDF文件。 我們來看一個例子。

在主函數中寫入以下程式碼:

using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // HTML to PDF Renderer
        IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

        // Inform about the printing operation
        命令列.WriteLine("Printing File");

        // Render URL as PDF
        using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");

        // Print the PDF document
        PdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
    }
}
using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // HTML to PDF Renderer
        IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

        // Inform about the printing operation
        命令列.WriteLine("Printing File");

        // Render URL as PDF
        using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");

        // Print the PDF document
        PdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

程式會將https://ironpdf.com/how-to/print-pdf/#advanced-printing解析為 PDF,然後Print()函數會使用預設印表機列印 PDF。

讓我們運行一下程式。

運行程式:

按 Ctrl + F5 運行解決方案。

螢幕上顯示的輸出

螢幕上顯示的輸出

Print()函數將使用預設印表機列印文件。 有時我們可能不想使用預設印表機。 在這種情況下,IronPDF 提供了一行程式碼來更改印表機名稱。

變更印表機名稱:

讓我們舉個例子來幫助我們更好地理解。 使用以下程式碼變更預設印表機:

using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Load the PDF document from file
        PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");

        // Change to a different printer by name
        pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";

        // Print the PDF document
        pdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
    }
}
using System; // Required for 命令列 operations

class Program
{
    static void Main()
    {
        // Load the PDF document from file
        PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");

        // Change to a different printer by name
        pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";

        // Print the PDF document
        pdfDocument.Print();

        // Print operation success message to console
        命令列.WriteLine("File Printed Successfully!");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

您可以看到,我只添加了一行額外的程式碼來指定印表機。 我以 Microsoft Print to PDF 為例進行示範。

運行程式:

按 Ctrl + F5 運行程式。

接下來會出現一個對話框,要求我們輸入要儲存的檔名,因為我們使用的是 Microsoft Print to PDF。

Csharp Print Pdf Files 10 related to 運行程式:

指定檔案名,然後按一下"儲存"按鈕。 我已指定"SamplePrint.Pdf"。 控制台將顯示以下輸出:

Csharp Print Pdf Files 11 related to 運行程式:

Microsoft Print to PDF 已在我的 D 盤中列印了一個 PDF 檔案。讓我們看看。

Csharp Print Pdf Files 12 related to 運行程式:

希望這篇教學對您有幫助,互動性強,而且容易理解。 您可以在下方評論區提出問題或提供回饋意見。

常見問題解答

如何使用 IronPDF 在 C# 中打印 PDF 文件?

要在 C# 中使用 IronPDF 列印 PDF 檔案,請使用 PdfDocument.FromFile 方法載入 PDF 文件,然後在載入的文件上呼叫 Print 方法。

IronPDF 可以用來依序列印多個 PDF 檔案嗎?

是的,IronPDF 可以通過遍歷文件路徑集合並為每個文件調用 Print 方法來打印多個 PDF 文件。

如何將 URL 轉換成 PDF,然後用 C# 來列印?

使用 IronPDF,使用 HtmlToPdf.RenderUrlAsPdf 方法將 URL 轉換為 PDF。然後在產生的 PDF 文件上使用 Print 方法來列印。

IronPDF 是否可以選擇特定的印表機來列印 PDF?

是的,您可以在使用 PdfDocument 類的 GetPrintDocument 方法時,透過設定 PrinterSettings 物件中的 PrinterName 屬性來選擇特定的印表機。

IronPDF 的 PDF 列印支援哪些平台?

IronPDF 支援在 .NET Framework、.NET Core 以及 .NET 5 和 6 應用程式上列印 PDF,並與 Windows、macOS、Android 和 iOS 平台相容。

如何使用 .NET 函式庫以 C# 開始列印 PDF?

若要開始使用 IronPDF 列印 PDF,請透過 Visual Studio 中的 NuGet 套件管理員安裝該函式庫,並依照 Iron Software 提供的說明文件和範例進行操作。

使用 PDF 進行文件共享有哪些好處?

PDF 在文件共享方面具有優勢,因為 PDF 重量輕、可在不同裝置上保持格式一致,而且大多數作業系統都支援 PDF。

使用 IronPDF 進行列印是否需要進階的 C# 技能?

使用 IronPDF 執行列印任務只需具備 C# 的基本知識即可,因為該函式庫提供了直接的方法和範例來協助開發人員。

Curtis Chau
技術作家

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

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