如何将 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、HTML 到 PDF 转换、PDF 操作(包括添加、删除和修改内容)、交互式表单处理、PDF 合并和拆分、加密和跨平台兼容性等功能简化了与 PDF 相关的任务,使其成为需要 PDF 文档管理和生成的广泛应用程序的宝贵工具。
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将两个PDF文件("PdfFile_1.pdf"和"PdfFile_2.pdf")从指定的基础路径("D:\PDFFiles")合并成一个名为"mergePdf.pdf"的PDF。通过打开每个输入的PDF,添加其内容到输出文档,然后关闭文档来完成这一过程。 上述代码将多个 PDF 合并为一个 PDF。
我们使用了两个输入文件,如下所示:

我们代码创建的新文件如下所示:

结论
在与iText的比较中,IronPDF在C#应用程序中的PDF文档合并方面优于iText。 虽然这两个库都具备能力,但 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,可以在包管理器控制台中执行命令'Install-Package IronPDF'来使用NuGet包管理器。
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在这些方面表现出色,成为首选。


