跳過到頁腳內容
產品比較

如何使用iTextSharp合併PDF文件

Full Comparison

Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.

View Full Comparison

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

如何使用 iTextSharp 合併 PDF 檔案

以下是如何使用 iTextSharp 合併 PDF 的逐步指南:

  1. 建立一個新的 Document 對象,並指定 PDF 檔案的基本路徑。
  2. 開啟 Document 進行編輯。 3.定義要合併的 PDF 檔案名稱陣列。
  3. 對於清單中的每個 PDF 文件,建立一個 PdfReader,將其內容新增至 PdfCopy 對象,然後關閉 PdfReader
  4. 關閉 Document,完成合併 PDF。

IronPDF。

IronPDF 適用於 .NET Webpage 是一個 .NET 函式庫,可讓開發人員在 C# 和 .NET 應用程式中建立、修改 PDF 文件,並與 PDF 文件互動。 它簡化了 PDF 相關任務,提供的功能包括從頭產生 PDF、HTML 至 PDF 轉換、PDF 操作(包括新增、移除和修改內容)、互動表單處理、PDF 合併和分割、加密以及跨平台相容性,使其成為需要管理和產生 PDF 文件的廣泛應用程式的重要工具。

iTextSharp。

iTextSharp已被 iText 7 取代,因為它已達到生命週期結束 (EOL),目前只有安全修復。 強烈建議使用 iText 7 或考慮將現有的轉換為新專案。 iText 7 提供了顯著的改進,包括 HTML 至 PDF 轉換、PDF 編輯、SVG 支援、更好的語言支援、除錯工具、資料萃取以及更多的模組化功能。 它簡化了 PDF 文件的處理,並提供 AGPL 和商業授權。

安裝 IronPDF Library

若要在您的 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) 中,例如 Visual Studio,開啟您要使用 iTextSharp 函式庫的 C# 專案。 2.前往 Tools > NuGet Package Manager > Package Manager Console。 3.在套件管理員控制台,執行下列指令:

Install-Package iTextSharp

此指令會告訴 NuGet (Visual Studio 的套件管理員) 下載 iTextSharp 套件及其相依套件,並將其安裝到您的專案中。

NuGet 將下載並安裝 iTextSharp 套件和任何所需的相依性。 您可以在 Package Manager Console 中監控安裝進度。

如何使用 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,將其內容添加到輸出文檔,然後關閉文檔來實現這一目標。 上述程式碼會將多個 PDF 合併為一個 PDF。

我們使用了以下兩個輸入檔案:

如何使用 iTextSharp 合併 PDF 檔案:圖 5 - 與先前相同的輸入 PDF。

我們的程式碼所建立的新檔案如下:

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

結論

與 iTextSharp 的比較中,IronPDF 成為在 C# 應用程式中合併 PDF 文件的上佳選擇。 雖然這兩個函式庫都很有能力,但 IronPDF 提供了更友善的使用者介面、現代化的功能(例如 HTML 到 PDF 的轉換)、明確的授權選項、透過 NuGet 的直接整合,以及積極的開發,共同簡化了合併流程、縮短了開發時間,並確保能為 PDF 相關任務提供更可靠的解決方案。 其友好的用戶界面、強大的功能集以及持續的開發使 IronPDF 成為用 C# 合併 PDF 的優秀解決方案。

請注意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 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

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