
如何在没有 Adobe 的情况下合并 PDF 文件
PDF(便携式文档格式)文件广泛用于共享和展示文档,有时您可能需要将PDF拆分为多个文件。 无论您是要提取特定页面、将大型文档分成更小的部分,还是为每章创建单独的文件,拆分PDF在多种情况下都很有用。
在本文中,我们将学习如何使用C#拆分PDF。 C#是一种多功能且强大的语言,有多个库可用于相对简单地操作PDF。 我们将研究以下两个用于在C#中拆分PDF的库。
如何使用C#中iText来拆分PDF
- 首先,安装iText库。
- 从输入PDF文件创建一个PdfReader。
- 使用PdfDocument来处理PDF内容。
- 计算每个拆分文件的页数。
- 设置初始页范围值。
- 使用循环处理每个拆分文件。
- 为当前的拆分文件创建一个新的PdfDocument。
- 将页面从原始文档复制到新文档。
- 更新下一次迭代的页范围值。
- 保存完成的输出PDF。
- 重复直到所有文件创建完毕。
- 继续处理指定数量的拆分文件。
IronPDF

IronPDF是一个强大的C#库,旨在处理PDF文件。 它提供了创建、修改和提取PDF文档内容的功能。 开发者可以从头生成PDF,编辑现有的PDF,并合并或拆分它们。 此外,IronPDF在将HTML内容转换为PDF格式方面表现出色,非常适合生成报表或文档。 IronPDF支持数字签名、安全功能和高质量输出,简化了.NET应用程序中的PDF相关任务。
iText

iText(iText)是一个广泛使用的库,专用于.NET框架中的PDF文件操作。 它为程序化地创建、修改和提取PDF文档内容提供了强大的功能。 开发者可以使用iText为PDF添加文本、图像、表格和其他图形元素。 此外,它支持文档组装、数字签名,并符合存档和可访问性标准。 iText最初是一个Java库,后来移植到.NET,并拥有一个活跃的开发者和用户社区。
安装IronPDF库
要在Visual Studio中使用Package Manager Console安装IronPDF NuGet包,请按照以下步骤操作:
-
在Visual Studio中,依次转到工具 -> NuGet包管理器 -> Package Manager Console。
-
使用以下命令来安装IronPDF NuGet包:
PM > Install-Package IronPdf
这将下载并安装IronPDF包及其依赖项到您的项目中。 一旦安装完成,您就可以在C#项目中开始使用IronPDF进行PDF相关的任务。
或者,您可以在Visual Studio中使用NuGet包管理器安装IronPDF,或直接将包添加到您的项目文件中。另一种选择是从官方网站下载包并手动添加到您的项目中。 每种方法都提供了一种将IronPDF集成到C#项目中的简便方法,以实现PDF相关功能。
安装iText库
要使用Visual Studio中的包管理器控制台安装iText,您可以按照以下步骤进行:
-
在Visual Studio中,依次转到工具 -> NuGet包管理器 -> Package Manager Console。
-
使用以下命令安装iText NuGet包:
Install-Package itext7SHELL
这将下载并安装iText包及其依赖项到您的项目中。 安装完成后,您可以开始在您的C#项目中使用iText处理PDF。
使用IronPDF在C#中拆分PDF文档
我们可以使用IronPDF将一个PDF文件拆分为多个PDF文件。 它提供了一种简单的方法来实现这一目标。 以下代码将把源PDF文件作为输入,并将其拆分为多个PDF文件。
using IronPdf;
class Program
{
static void Main(string[] args)
{
string file = "input.pdf";
// The folder to save the split PDFs
string outputFolder = "output_split";
int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into
// Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles);
}
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int numberOfSplitFiles)
{
// Load the input PDF
PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
// Initialize page range values
int firstPage = 1;
int lastPage = 2;
int totalPageInOneFile = sourceFile.PageCount / numberOfSplitFiles;
for (int i = 1; i <= numberOfSplitFiles; i++)
{
// Copy multiple pages into a new document
PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage, lastPage);
// Generate the output file path
string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
// Save the new split PDF
newSplitPDF.SaveAs(name);
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}Imports IronPdf
Class Program
Shared Sub Main(args As String())
Dim file As String = "input.pdf"
' The folder to save the split PDFs
Dim outputFolder As String = "output_split"
Dim numberOfSplitFiles As Integer = 3 ' Specify how many parts you want to split the PDF into
' Call the SplitPdf method to split the PDF
SplitPdfUsingIronPDF(file, outputFolder, numberOfSplitFiles)
End Sub
Shared Sub SplitPdfUsingIronPDF(inputPdfPath As String, outputFolder As String, numberOfSplitFiles As Integer)
' Load the input PDF
Dim sourceFile As PdfDocument = PdfDocument.FromFile(inputPdfPath)
' Initialize page range values
Dim firstPage As Integer = 1
Dim lastPage As Integer = 2
Dim totalPageInOneFile As Integer = sourceFile.PageCount \ numberOfSplitFiles
For i As Integer = 1 To numberOfSplitFiles
' Copy multiple pages into a new document
Dim newSplitPDF As PdfDocument = sourceFile.CopyPages(firstPage, lastPage)
' Generate the output file path
Dim name As String = $"{outputFolder}\SplitPDF_IronPDF_{i}.pdf"
' Save the new split PDF
newSplitPDF.SaveAs(name)
' Update page range values for the next iteration
firstPage = lastPage + 1
lastPage += totalPageInOneFile
Next
End Sub
End Class代码解释
此代码的目的是使用IronPDF库将给定的PDF文件拆分为多个较小的PDF文件。 定义SplitPdfUsingIronPDF方法以实现此功能。
方法参数
inputPdfPath:一个字符串,表示输入PDF文件的路径(如"input.pdf")。outputFolder:一个字符串,表示拆分的PDF文件将保存的输出文件夹(如"output_split")。numberOfSplitFiles:一个整数,指示原始PDF将被拆分为多少个较小的PDF文件。
拆分过程
在SplitPdfUsingIronPDF方法内:
- 通过加载指定的
PdfDocument对象。 - 初始化两个整数变量,
lastPage。 这代表了拆分的页范围。 - 通过将源PDF的总页数除以指定的
totalPageInOneFile。 - 循环从1到
numberOfSplitFiles: - 创建新的
newSplitPDF。 - 将
newSplitPDF。 - 生成的较小PDF以"SplitPDF_IronPDF_1.pdf"(用于第一次拆分)的文件名保存在指定的输出文件夹中。
- 更新
lastPage值以供下次迭代。
注意
您应该将"input.pdf"替换为输入PDF文件的实际路径。确保IronPDF库已正确安装并在您的项目中引用。
输出文件创建为:

使用iText在C#中拆分PDF
现在,我们将使用iText来将我们的PDF文档拆分为多个PDF文件。 以下代码将把源文件作为输入,并将该PDF文档拆分为多个较小的文件。
using System;
using iText.Kernel.Pdf;
class Program
{
static void Main(string[] args)
{
string inputPath = "input.pdf";
// Output PDF files path (prefix for the generated files)
string outputPath = "output_split";
int numberOfSplitFiles = 3; // Specify how many parts you want to split the PDF into
// Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, numberOfSplitFiles);
}
static void SplitPdfUsingiTextSharp(string inputPdfPath, string outputFolder, int numberOfSplitFiles)
{
using (PdfReader reader = new PdfReader(inputPdfPath))
{
using (PdfDocument doc = new PdfDocument(reader))
{
// Calculate the number of pages for each split file
int totalPageInOneFile = doc.GetNumberOfPages() / numberOfSplitFiles;
int firstPage = 1;
int lastPage = totalPageInOneFile;
for (int i = 1; i <= numberOfSplitFiles; i++)
{
// Generate the output file path
string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
// Create a new document and attach a writer for the specified output file
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename)))
{
// Copy pages from the original document to the new document
doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
}
// Update page range values for the next iteration
firstPage = lastPage + 1;
lastPage += totalPageInOneFile;
}
}
}
}
}Imports System
Imports iText.Kernel.Pdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim inputPath As String = "input.pdf"
' Output PDF files path (prefix for the generated files)
Dim outputPath As String = "output_split"
Dim numberOfSplitFiles As Integer = 3 ' Specify how many parts you want to split the PDF into
' Call the SplitPdf method to split the PDF
SplitPdfUsingiTextSharp(inputPath, outputPath, numberOfSplitFiles)
End Sub
Private Shared Sub SplitPdfUsingiTextSharp(ByVal inputPdfPath As String, ByVal outputFolder As String, ByVal numberOfSplitFiles As Integer)
Using reader As New PdfReader(inputPdfPath)
Using doc As New PdfDocument(reader)
' Calculate the number of pages for each split file
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Dim totalPageInOneFile As Integer = doc.GetNumberOfPages() / numberOfSplitFiles
Dim firstPage As Integer = 1
Dim lastPage As Integer = totalPageInOneFile
For i As Integer = 1 To numberOfSplitFiles
' Generate the output file path
Dim filename As String = $"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"
' Create a new document and attach a writer for the specified output file
Using pdfDocument As New PdfDocument(New PdfWriter(filename))
' Copy pages from the original document to the new document
doc.CopyPagesTo(firstPage, lastPage, pdfDocument)
End Using
' Update page range values for the next iteration
firstPage = lastPage + 1
lastPage += totalPageInOneFile
Next i
End Using
End Using
End Sub
End Class代码解释
此代码演示如何使用iText将大型PDF文件拆分为较小的块。 每个较小的PDF文档都将包含原始文档的一部分。
使用iText拆分PDF
- 主要功能封装在
SplitPdfUsingiTextSharp方法中。 - 上述方法采用三个参数:
numberOfSplitFiles。
使用PdfReader和PdfDocument
在SplitPdfUsingiTextSharp方法内:
- 通过加载指定的
PdfReader对象。 - 使用
PdfDocument对象。 - 将源PDF的总页数除以
numberOfSplitFiles以确定每个较小的PDF应该包含多少页。
拆分过程
循环从1到numberOfSplitFiles:
- 创建一个名为"SplitPDF_iText_1.pdf"(用于首次拆分)的新较小PDF文件,在指定的输出文件夹中。
- 在这个新的PDF文档(
doc复制页面: lastPage(含)被复制。- 保存较小的PDF。
- 对于下次迭代,更新
lastPage的值。
注意
确保在您的项目中正确安装和引用iText库。 将"input.pdf"替换为您的输入PDF文件的实际路径。

比较
为了使用iText和IronPDF比较两种拆分方法,让我们根据几个因素进行评估:
- 易用性:
- **iText:**iText方法涉及创建PdfReader、PdfDocument和PdfWriter。 它计算页范围并将页面复制到新文档。 此方法要求理解iText API。
- IronPDF: IronPDF方法涉及从源文件创建PdfDocument、复制页面和保存到新文件。其API更简单。
- 代码可读性:
- **iText:**代码包含更多步骤,使其更长且可能更复杂。
- IronPDF: 代码简洁且可读性更强,由于步骤和方法调用较少。
- 性能:
- **iText:**性能可能受到多次创建和销毁PdfDocument实例的影响。
- IronPDF: 方法涉及较少的步骤并通过高效的页面复制提供更好的性能。
- 内存使用:
- **iText:**创建多个PdfDocument实例将消耗更多内存。
- **IronPDF:**由于其简化的方法,该方法更具内存效率。
结论
总之,iText和IronPDF都为在C#中拆分PDF文件提供了强大的解决方案,各有优劣。 IronPDF 以其简单性、可读性和潜在的更好性能而脱颖而出,因为其方法更加直接。
寻求平衡多功能性和易用性的开发者可能会发现IronPDF是一个引人注目的选择。此外,IronPDF 提供免费试用。 最终,在iText和IronPDF之间的选择取决于个人项目的需求和首选的开发风格。

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.
Related Articles

