跳至頁尾內容
USING IRONPRINT

using IronPDF 在 C# 中列印 PDF 文件

PDF是最常用的文件格式。 它輕量且大多數作業系統支援。 PDF文件可以很容易地在裝置間共享。 它們提供了良好的閱讀體驗,因為它們在任何介面和操作系統上都顯示相同。 PDF可以從包括手機、平板等在內的所有裝置上存取。

它們的普及性要求所有C#開發者都知道如何使用C#列印PDF文件。 這是因為您可能會遇到需要列印文件(如發票、工資單、訂單單或個人檔案等)時的問題。這些文件通常是PDF文件格式,因此我們必須知道如何使用C#列印PDF文件。

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

我將使用IronPDF程式庫來列印PDF文件。 相信我,您會喜歡的!


您將從本文中獲得什麼?

閱讀本文後,您將能夠執行以下任務:

  • 使用C#列印單個PDF文件
  • 使用C#一次列印多個PDF文件
  • 將URL轉換為PDF,然後列印它。

您必須具備基本的C#編程知識,以便更好地理解本教程。

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


IronPDF:

IronPDF是一個.NET程式庫,為我們提供了閱讀、建立、操作和列印PDF文件的最簡單方法。 此程式庫可用於C#和VB .NET應用程式。 它可以輕鬆地與ASP .NET、ASP .NET core、MVC、Web Forms和Web APIs在.NET和.NET core上運作。 它允許使用者下載PDF文件,通過電郵發送並將其儲存在雲端。

您可以使用此連結來探索更多有關IronPDF的資訊。

讓我們開始演示。

打開Visual Studio

打開Visual Studio。 建立一個新項目,或者如果您要將此功能新增到現有軟體中,請打開現有項目。

我在此教程中使用了控制台應用程式; 您可以根據您的軟體需求選擇任何其他的選項。

Project Created

項目已建立。

安裝NuGet套件

通過單擊工具欄頂部的工具 > NuGet套件管理器 > 套件管理器控制台打開控制台。

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

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

NuGet套件管理器控制台

NuGet套件管理器控制台

在控制台中輸入以下命令來安裝IronPDF NuGet套件,然後按回車鍵。

Install-Package IronPrint

NuGet套件將如下面所示被安裝:

安裝NuGet套件

安裝NuGet套件


Print PDF Files in C

現在,我們來考慮一個範例來演示如何使用IronPDF程式庫在C#中列印PDF文件。

打開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
        Console.WriteLine("File Printed Successfully!");
        Console.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
        Console.WriteLine("File Printed Successfully!");
        Console.ReadKey();
    }
}
Imports System ' Required for 控制台 operations

Class Program
    Shared Sub Main()
        ' Load the PDF document from file
        Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
            ' Print the loaded PDF document to the default printer
            pdfDocument.Print()
        End Using

        ' Print operation success message to console
        Console.WriteLine("File Printed Successfully!")
        Console.ReadKey()
    End Sub
End Class
$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運行程式。

控制台

控制台


Print Multiple PDF Documents in C

我將向您展示如何使用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
            Console.WriteLine("Printing File: {0}.pdf", i);

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

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

        Console.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
            Console.WriteLine("Printing File: {0}.pdf", i);

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

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

        Console.ReadKey();
    }
}
Imports System ' Required for 控制台 operations

Class Program
    Shared Sub Main()
        ' Loop through each file number and print
        For i As Integer = 1 To 14
            ' Load each PDF document from file
            Using pdfDocument As PdfDocument = PdfDocument.FromFile($"D:\Print\{i}.pdf")
                ' Inform which file is being printed
                Console.WriteLine("Printing File: {0}.pdf", i)

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

                ' Print operation success message to console
                Console.WriteLine("{0}.pdf Printed Successfully!", i)
            End Using
        Next

        Console.ReadKey()
    End Sub
End Class
$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
        Console.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
        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
        Console.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
        Console.WriteLine("File Printed Successfully!");
    }
}
Imports System ' Required for 控制台 operations

Class Program
    Shared Sub Main()
        ' HTML to PDF Renderer
        Dim Renderer As New IronPdf.HtmlToPdf()

        ' Inform about the printing operation
        Console.WriteLine("Printing File")

        ' Render URL as PDF
        Using PdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing")
            ' Print the PDF document
            PdfDocument.Print()
        End Using

        ' Print operation success message to console
        Console.WriteLine("File Printed Successfully!")
    End Sub
End Class
$vbLabelText   $csharpLabel

此程式將把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
        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
        Console.WriteLine("File Printed Successfully!");
    }
}
Imports System ' Required for 控制台 operations

Class Program
    Shared Sub Main()
        ' Load the PDF document from file
        Dim pdfDocument As 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
        Console.WriteLine("File Printed Successfully!")
    End Sub
End Class
$vbLabelText   $csharpLabel

您可以看到我只新增了一行附加程式碼來指定列印機。 我已指定微軟Print to PDF作為演示。

運行程式:

通過按下Ctrl + F5運行程式。

將出現以下對話框,詢問我們輸入文件名以保存,因為我們正在使用微軟Print to PDF。

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

指定文件名然後點擊"儲存"按鍵。 我已指定"SamplePrint.Pdf"。 將顯示以下控制台輸出:

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

微軟Print to PDF已在我的D驅動器列印了一個PDF文件。讓我們來看看它。

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

我希望這個教程對您有幫助,互動性強且易於理解。 您可以在下面的評論區對本教程提出問題或給予反饋。

常見問題

如何在C#中使用IronPDF列印PDF檔案?

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

IronPDF可以用於順序列印多個PDF檔案嗎?

是的,IronPDF可以通過迴圈處理文件路徑集合,並對每個文件調用Print方法來列印多個PDF檔案。

如何將URL轉換為PDF,然後在C#中列印?

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

是否可以使用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在文件共享方面具有優勢,因為它們輕量、在不同裝置上保持一致的格式,且大多數操作系統都支持。

我需要高級的C#技能來使用IronPDF進行列印嗎?

基本的C#知識已足以使用IronPDF進行列印任務,因為該程式庫提供了簡單的方法和範例來輔助開發者。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

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