跳過到頁腳內容
產品比較

如何使用iTextSharp合併PDF文件

合併 PDF 文件是各種軟體應用程式中的常見需求,例如文件管理系統、報表生成工具等。 .NET 生態系統中,開發人員有多個庫可用來操作 PDF 文件。 iTextSharp 和 IronPDF 是在 C# 應用程式中處理 PDF 的兩個熱門選擇。 在本文中,我們將探討如何使用 iTextSharp 合併 PDF 並將其與 IronPDF 進行比較,以幫助您在選擇 PDF 操作需求的庫時做出明智決策。

如何使用 iTextSharp 合併 PDF 文件

這是一個關於如何使用 iTextSharp 合併 PDF 的逐步指南:

  1. 建立一個新的 Document 對象並為您的 PDF 文件指定一個基路徑。
  2. 開啟 Document 以進行編輯。
  3. 定義一個要合併的 PDF 文件名稱的陣列。
  4. 對於列表中的每個 PDF 文件,創建一個 PdfReader,將其內容添加到 PdfCopy 對象,然後關閉 PdfReader
  5. 關閉 Document,完成合併 PDF。

IronPDF

IronPDF 網頁 是一個 .NET 庫,使開發人員可以在他們的 C# 和 .NET 應用程式中創建、修改和交互 PDF 文件。 它通過提供從零開始生成 PDF、HTML 到 PDF 轉換、PDF 操作(包括添加、刪除和修改內容)、交互式表單處理、PDF 合併和拆分、加密和跨平台兼容性等功能,簡化了 PDF 相關的任務,是需要 PDF 文件管理和生成的廣泛應用程式的一個有價值的工具。

iTextSharp

iTextSharp 已被 iText 7 取代,因為它已達到終止生命周期 (EOL),僅提供安全修復。 強烈建議使用 iText 7 或考慮將現有項目過渡到新項目。 iText 7 提供顯著改進,包括 HTML 到 PDF 轉換、PDF 涂黑、SVG 支持、語言支持更好、調試工具、數據提取和更多模組化功能。 它簡化了 PDF 文件處理,並提供 AGPL 和商業許可證。

安裝IronPDF庫

要在您的 Visual Studio 項目中安裝 IronPDF NuGet 套件,您可以遵循以下步驟:

  1. 首先在 Visual Studio 中打開您想要使用 IronPDF 庫的專案。
  2. 如果您正在使用 Visual Studio,請轉到工具 > NuGet 包管理器 > 包管理控制台。
  3. 在包管理控制台中運行以下命令以安裝 IronPDF:
Install-Package IronPdf

Visual Studio 將下載並安裝該套件及其依賴項。 您可以在輸出窗口中監控進度。 完成安裝後,您可以在 C# 代碼中開始使用 IronPDF。

**如何使用 iTextSharp 合併 PDF 文件:圖 1 - 完成安裝顯示在 Visual Studio 中的內置終端中。**

安裝成功後,您現在可以在您的項目中開始使用 IronPDF。 在您的代碼文件中包含必要的 "using" 聲明並開始使用 IronPDF 的功能處理 PDF。

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

您現在可以訪問 IronPDF 提供的功能,用於在您的 C# 項目中處理 PDF 文件。 記得保存您的項目並編譯,以確保庫正確集成。

安裝 iTextSharp PDF 庫

要在 C# 項目中安裝 iTextSharp PDF 庫,請遵循以下步驟:

  1. 在您所在的首選集成開發環境 (IDE) 中開啟要使用 iTextSharp 庫的 C# 項目(例如 Visual Studio)。
  2. 前往工具 > NuGet 包管理器 > 包管理控制台。
  3. 在包管理控制台中運行以下命令:
Install-Package iTextSharp

此命令告訴 NuGet(Visual Studio 的包管理器)下載並安裝 iTextSharp 套件及其依賴項到您的項目中。

NuGet 會下載並安裝 iTextSharp 套件及任何需要的依賴項。 您可以在包管理控制台中監控安裝進度。

**如何使用 iTextSharp 合併 PDF 文件**:圖 2 - 完整的安裝顯示在 Visual Studio 中的內置終端中。

完成安裝後,您會在包管理控制台中看到一條確認信息,指示 iTextSharp 套件已成功安裝。 安裝成功後,您現在可以開始在項目中使用 iTextSharp。 在您的代碼文件中包含必要的 using 聲明並開始使用 iTextSharp 的功能處理 PDF。

使用 IronPDF 合併多個 PDF 文件到一個 PDF 文檔

IronPDF 提供了一種直接的合併多個 PDF 文件到一個 PDF的方法。 IronPDF 在合併 PDF 文檔方面提供了極大的靈活性。 以下是將多個 PDF 文件合併為單個 PDF 文件的示例代碼:

using IronPdf;
using System.Collections.Generic;

static void Main(string[] args)
{
    string basePath = @"D:\PDFFiles\";
    string[] pdfFiles = { "PdfFile_1.pdf", "PdfFile_2.pdf" };

    // Create a list to hold the PDF documents to be merged
    List<PdfDocument> docList = new List<PdfDocument>();

    // Add each PDF to the list
    foreach (string filename in pdfFiles)
    {
        docList.Add(new PdfDocument(basePath + filename));
    }

    // Merge the PDFs into one document
    var mergedPDF = PdfDocument.Merge(docList);

    // Save the merged PDF to the specified path
    mergedPDF.SaveAs(basePath + "mergePDFbyIronPDF.pdf");
}
using IronPdf;
using System.Collections.Generic;

static void Main(string[] args)
{
    string basePath = @"D:\PDFFiles\";
    string[] pdfFiles = { "PdfFile_1.pdf", "PdfFile_2.pdf" };

    // Create a list to hold the PDF documents to be merged
    List<PdfDocument> docList = new List<PdfDocument>();

    // Add each PDF to the list
    foreach (string filename in pdfFiles)
    {
        docList.Add(new PdfDocument(basePath + filename));
    }

    // Merge the PDFs into one document
    var mergedPDF = PdfDocument.Merge(docList);

    // Save the merged PDF to the specified path
    mergedPDF.SaveAs(basePath + "mergePDFbyIronPDF.pdf");
}
Imports IronPdf
Imports System.Collections.Generic

Shared Sub Main(ByVal args() As String)
	Dim basePath As String = "D:\PDFFiles\"
	Dim pdfFiles() As String = { "PdfFile_1.pdf", "PdfFile_2.pdf" }

	' Create a list to hold the PDF documents to be merged
	Dim docList As New List(Of PdfDocument)()

	' Add each PDF to the list
	For Each filename As String In pdfFiles
		docList.Add(New PdfDocument(basePath & filename))
	Next filename

	' Merge the PDFs into one document
	Dim mergedPDF = PdfDocument.Merge(docList)

	' Save the merged PDF to the specified path
	mergedPDF.SaveAs(basePath & "mergePDFbyIronPDF.pdf")
End Sub
$vbLabelText   $csharpLabel

上面的代碼使用 IronPDF 庫合併位於指定基路徑(“D:\PDFFiles”)的兩個 PDF 文件("PdfFile_1.pdf" 和 "PdfFile_2.pdf")。 它創建一個 PdfDocument 對象的列表,將輸入的 PDF 添加到該列表中,使用 PdfDocument.Merge 將它們合併為一個 PDF,並將該合併的 PDF 保存為 "mergePDFbyIronPDF.pdf" 在相同的基路徑。

以下是此示例中使用的樣本 PDF:

**如何使用 iTextSharp 合併 PDF 文件**:圖 3 - 一個簡單的 PDF 文件,具有標題“簡單的 PDF 文件”和一些正文文字,以及一篇單頁 PDF 文章。

以下是合併的 PDF 文件:

**如何使用 iTextSharp 合併 PDF 文件**:圖 4 - 合併的 PDF,首先是簡單的 PDF,然後是文章。

使用 iTextSharp 合併多個 PDF 文件

iTextSharp 沒有提供直接的方法來合併 PDF 文件。 不過,我們可以通過打開每個輸入的 PDF 文件並將其內容添加到輸出文檔來實現這一點。 以下示例代碼將 PDF 文件合併為一個 PDF 文檔:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

static void Main(string[] args)
{
    // Create a new Document
    Document doc = new Document();

    string basePath = @"D:\PDFFiles\";

    // Create a PdfCopy instance to copy pages from source PDFs
    PdfCopy copy = new PdfCopy(doc, new FileStream(basePath + "mergePdf.pdf", FileMode.Create));

    // Open the document for writing
    doc.Open();

    string[] pdfFiles = { "PdfFile_1.pdf", "PdfFile_2.pdf" };

    // Loop through all the PDF files to be merged
    foreach (string filename in pdfFiles)
    {
        // Read the content of each PDF
        PdfReader reader = new PdfReader(basePath + filename);

        // Add the document to PdfCopy
        copy.AddDocument(reader);

        // Close the reader
        reader.Close();
    }

    // Close the Document
    doc.Close();
}
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

static void Main(string[] args)
{
    // Create a new Document
    Document doc = new Document();

    string basePath = @"D:\PDFFiles\";

    // Create a PdfCopy instance to copy pages from source PDFs
    PdfCopy copy = new PdfCopy(doc, new FileStream(basePath + "mergePdf.pdf", FileMode.Create));

    // Open the document for writing
    doc.Open();

    string[] pdfFiles = { "PdfFile_1.pdf", "PdfFile_2.pdf" };

    // Loop through all the PDF files to be merged
    foreach (string filename in pdfFiles)
    {
        // Read the content of each PDF
        PdfReader reader = new PdfReader(basePath + filename);

        // Add the document to PdfCopy
        copy.AddDocument(reader);

        // Close the reader
        reader.Close();
    }

    // Close the Document
    doc.Close();
}
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Shared Sub Main(ByVal args() As String)
	' Create a new Document
	Dim doc As New Document()

	Dim basePath As String = "D:\PDFFiles\"

	' Create a PdfCopy instance to copy pages from source PDFs
	Dim copy As New PdfCopy(doc, New FileStream(basePath & "mergePdf.pdf", FileMode.Create))

	' Open the document for writing
	doc.Open()

	Dim pdfFiles() As String = { "PdfFile_1.pdf", "PdfFile_2.pdf" }

	' Loop through all the PDF files to be merged
	For Each filename As String In pdfFiles
		' Read the content of each PDF
		Dim reader As New PdfReader(basePath & filename)

		' Add the document to PdfCopy
		copy.AddDocument(reader)

		' Close the reader
		reader.Close()
	Next filename

	' Close the Document
	doc.Close()
End Sub
$vbLabelText   $csharpLabel

上面的代碼使用 iTextSharp 將指定基路徑("D:\PDFFiles")中的兩個 PDF 文件("PdfFile_1.pdf" 和 "PdfFile_2.pdf")合併為一個名為 "mergePdf.pdf" 的單個 PDF。 它通過打開每個輸入的 PDF,將其內容添加到輸出文檔,然後關閉文檔來實現這一點。 上面的代碼會將多個 PDFs 合併為一個 PDF。

我們使用了以下兩個輸入文件:

**如何使用 iTextSharp 合併 PDF 文件**:圖 5 - 與之前相同的輸入 PDF 文件。

我們代碼創建的新文件如下:

**如何使用 iTextSharp 合併 PDF 文件**:圖 6 - 合併的 PDF,首先是簡單的 PDF,然後是文章。

結論

iTextSharp 比較,IronPDF 在 C# 應用程式中合併 PDF 文檔時成為更優秀的選擇。 雖然這兩個庫都是可以的,但 IronPDF 提供了一個更加用戶友好的介面,現代功能如 HTML 到 PDF 轉換,明確的許可選項,通過 NuGet 直接集成,和積極的開發,整體上簡化了合併過程,減少了開發時間,並確保 PDF 相關任務的更可靠解決方案。 其用戶友好的介面,強大的功能集和持續的開發使得 IronPDF 成為合併 PDF 的優越解決方案,尤其是在 C# 中。

請注意iText 7 是其各自擁有者的註冊商標。 本網站與 iText 7 無關,未受其認可或贊助。所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

我如何在 C# 中合併 PDF 文件?

您可以在 C# 中使用 IronPDF 來合併 PDF 文件,方法是創建 PdfDocument 物件列表,然後使用 PdfDocument.Merge 方法將它們合併為一個 PDF 文檔。

使用 IronPDF 进行 PDF 操作的好处是什么?

IronPDF 提供了用戶友好的介面,現代功能如 HTML 到 PDF 轉換、互動表單和跨平台兼容性,使其成為 C# 應用程式中 PDF 操作的高效工具。

我如何在 Visual Studio 中為 C# 項目安裝 PDF 庫?

要在 Visual Studio C# 項目中安裝 IronPDF,請使用 NuGet Package Manager 在分頁管理控制台中執行 'Install-Package IronPDF' 命令。

iTextSharp 和 IronPDF 合併 PDF 的區別是什麼?

iTextSharp 需要手動添加每個 PDF 文檔的內容,而 IronPDF 提供了一個更簡單的解決方案,具有直接的合併 PDF 的方法,減少了開發時間和複雜性。

為什麼開發人員應該從 iTextSharp 過渡到 iText 7 或 IronPDF?

開發人員應該過渡是因為 iTextSharp 已經到了生命周期的終點,而 iText 7 提供了改進的功能。然而,IronPDF 提供了更現代的功能和易用性,讓其成為 PDF 操作的更優選擇。

IronPDF 可以處理的不僅僅是合併 PDF 文件嗎?

是的,IronPDF 支持廣泛的 PDF 操作,包括從頭生成 PDF,將 HTML 轉換為 PDF,修改內容,處理表單和應用加密。

IronPDF 作為 C# 中 PDF 操作的可靠選擇的原因是什麼?

IronPDF 目前正在積極開發,提供現代功能並提供用戶友好的界面,簡化了 PDF 操作任務,使其成為開發人員的一個可靠和高效的選擇。

比起舊的庫,IronPDF 如何改善合併 PDF 的過程?

IronPDF 通過提供直接方法來合併 PDF 文件,優化了與 C# 項目的整合,並減少了整體開發時間。

開發人員在為 C# 選擇 PDF 庫時應該考慮哪些因素?

開發人員在選擇 C# 項目的 PDF 庫時應該考慮易用性、功能集、授權選項、兼容性和主動開發支持等因素。IronPDF 在這些方面表現出色,讓其成為首選。

Curtis Chau
技術作家

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

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