如何將多個PDF文件合併為一個文件
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.
在各種軟體應用中,合併PDF文件是一項常見需求,例如文件管理系統、報表生成工具等。 在.NET生態系中,開發者有多個程式庫可以用來操作PDF文件。 iText和IronPDF是在C#應用程式中處理PDF的兩個熱門選擇。 在本文中,我們將探討如何使用iText合併PDF,並與IronPDF進行比較,以幫助您在選擇PDF操作程式庫時能夠做出明智的決策。
如何使用iText合併PDF文件
以下是使用iText合併PDF的步驟指南:
- 建立一個新的
Document物件,並指定PDF文件的基本路徑。 - 打開
Document進行編輯。 - 定義一個要合併的PDF文件名陣列。
- 對於清單中的每個PDF文件,建立
PdfReader。 - 關閉
Document,完成合併PDF的操作。
IronPDF
IronPDF網頁是.NET程式庫,使開發者能夠在其C#和.NET應用中建立、修改和操作PDF文件。 它簡化了與PDF相關的任務,提供從頭生成PDF、HTML轉PDF、PDF操作(包括新增、刪除和修改內容)、互動表單處理、PDF合併和拆分、加密以及跨平台相容等功能,成為需要PDF文件管理和生成的各種應用的寶貴工具。
iText
iText已被iText取代,因為iText已達到其服役終止(EOL),僅會提供安全修復。 強烈建議使用iText或考慮在新項目中轉換現有項目。 iText提供了顯著的改進,包括HTML轉PDF、PDF修訂、SVG支持、更好的語言支持、除錯工具、資料提取及更模塊化的功能。 它簡化了PDF文件操作,並可用於AGPL和商業授權。
安裝 IronPDF 程式庫
要在Visual Studio項目中安裝IronPDF NuGet套件,您可以按照以下步驟進行:
- 首先打開您想在其中使用IronPDF程式庫的Visual Studio項目。
- 如果您使用Visual Studio,請到工具>NuGet套件管理器>套件管理器控制台。
- 在套件管理器控制台中運行以下命令安裝IronPDF:
Install-Package IronPdf
Visual Studio會下載並安裝該套件及其依賴項。 您可以在輸出視窗中監控進度。 安裝完成後,您可以在C#程式碼中開始使用IronPDF。

成功安裝IronPDF後,現在可以在您的項目中開始使用它。 在您的程式碼文件中包含必要的"using"語句,並開始使用IronPDF的功能操作PDF。
using IronPdf;
using IronPdf;
Imports IronPdf
您現在可以存取IronPDF提供的特性和功能,在您的C#項目中操作PDF文件。 請記得保存並構建您的項目,以確保該程式庫正確整合。
安裝iText PDF程式庫
要在C#項目中安裝iText PDF程式庫,請按照以下步驟:
- 打開您想在其中使用iText程式庫的C#項目,選擇您的首選整合開發環境(IDE),如Visual Studio。
- 前往工具 > NuGet套餐管理器 > 套餐管理器控制台。
- 在套件管理器控制台中運行以下命令:
Install-Package iTextSharp
此命令告訴NuGet(Visual Studio的套件管理器)下載並安裝iText套件及其依賴項到您的項目中。
NuGet將下載並安裝iText套件及所需的依賴項。 您可以在套件管理器控制台中監控安裝進度。

安裝完成後,您將在套件管理器控制台中看到一個確認消息,表示iText套件已成功安裝。 成功安裝iText後,您現在可以在項目中開始使用它。 在您的程式碼文件中包含必要的using語句,並開始使用iText的功能處理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
上面的程式碼使用IronPDF程式庫合併位於指定基本路徑("D:\PDFFiles")中的兩個PDF文件("PdfFile_1.pdf"和"PdfFile_2.pdf")。 它建立一個PdfDocument.Merge將其合併為一個PDF,並將合併後的PDF保存為"mergePDFbyIronPDF.pdf"在同一基本路徑中。
以下是此範例中使用的PDF範本:

以下是合併後的PDF文件:

使用iText合併多個PDF文件
iText不提供直接合併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
上述程式碼使用iText將指定基本路徑("D:\PDFFiles")中的兩個PDF文件("PdfFile_1.pdf"和"PdfFile_2.pdf")合併為一個名為"mergePdf.pdf"的PDF。它通過打開每個輸入的PDF,將其內容新增到輸出文件,然後關閉文件來完成此操作。 上述程式碼會將多個PDF合併為一個PDF。
我們使用如下的兩個輸入文件:

我們的程式碼建立的新文件如下:

結論
與iText相比,IronPDF成為在C#應用中合併PDF文件的優勢選擇。 雖然兩個程式庫都具備能力,但IronPDF提供更友好的介面,現代化的功能如HTML轉PDF、清晰的許可選項、通過NuGet簡便的整合及積極的開發,集體簡化了合併過程,縮短了開發時間,並確保了PDF相關任務的更可靠解決方案。 其使用者友好的介面、強大的功能集和持續的開發使IronPDF成為在C#中合併PDF的卓越解決方案。
常見問題
如何在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,在Package Manager Console中執行命令'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程式庫時應考慮哪些因素?
開發者應考慮使用的易用性、功能集、授權選項、相容性以及積極的開發支持等因素。IronPDF在這些方面表現出色,使其成為首選。



