产品比较

在 C# 中使用 iTextSharp 和 IronPDF 分割 PDF 的比较

发布 2024年三月6日
分享:

PDF (便携式文档格式) 文件被广泛用于共享和展示文档,有时您可能需要将 PDF 文件分割成多个文件。 无论您是要提取特定页面、将大型文档划分为较小的部分,还是要为每个章节创建单独的文件,在各种情况下,分割 PDF 都是一项非常有价值的任务。

在本文中,我们将学习如何使用 C# 来分割 PDF。 C# 是一门用途广泛、功能强大的语言,而且有几个库可以相对直接地操作 PDF。 我们将探讨以下两个用于在 C# 中分割 PDF 的库。

  1. iTextSharp

  2. IronPDF

如何使用 ITextSharp 在 C#&num 中分割 PDF

  1. 首先安装 iText7 库

  2. 从输入的 PDF 文件创建一个 PdfReader。

  3. 使用 PdfDocument 来处理 PDF 内容。

  4. 计算每个分割文件的页数。

  5. 设置初始页面范围值。

  6. 使用循环处理每个分割文件。

  7. 为当前分割文件创建一个新的 PdfDocument。

  8. 将原始文档中的页面复制到新文档中。

  9. 为下一次迭代更新页面范围值。

  10. 保存已完成输出的 PDF。

  11. 重复,直到创建完所有文件:

  12. 继续处理指定数量的分割文件。

IronPDF

iTextSharp 和 IronPDF 在 C# 中分割 PDF 的比较:图 1 - IronPDF 网页

IronPDF 是一个功能强大的 C# 库,专为处理 PDF 文件而设计。 它的功能包括 创造, 修改提取 PDF 文档中的内容。 开发人员可以从头开始生成 PDF 文件、编辑现有 PDF 文件,以及 合并或拆分 他们 此外,IronPDF 还擅长将 HTML 内容转换为 PDF 格式,因此在生成报告或文档时非常有用。 IronPDF 支持数字签名、安全功能和高质量输出,简化了 .NET 应用程序中与 PDF 相关的任务。

iTextSharp

iTextSharp 和 IronPDF 在 C# 中分割 PDF 的比较:图 2 - iTextSharp 网页

iTextSharp (iText 7) 是一个广泛使用的库,用于在 .NET Framework 中处理 PDF 文件。 它提供了强大的功能,可通过编程创建、修改和提取 PDF 文档中的内容。 开发人员可以使用 iTextSharp 在 PDF 中添加文本、图像、表格和其他图形元素。 此外,它还支持文档组装、数字签名,并符合存档和可访问性标准。 iTextSharp 最初是一个 Java 库,后被移植到 .NET 中,并拥有一个活跃的开发者和用户社区。

安装 IronPDF 库

安装 IronPDF NuGet 软件包 使用 Visual Studio 中的软件包管理器控制台,请按照以下步骤操作:

  1. 在 Visual Studio 中,转到工具 -> NuGet 包管理器 -> 包管理器控制台。

  2. 使用以下命令安装 IronPDF NuGet 软件包:
    :ProductInstall

这将下载并安装 IronPDF 软件包及其依赖项到您的项目中。 安装完成后,您就可以开始在 C# 项目中使用 IronPDF 执行 PDF 相关任务了。

另外,您也可以使用 Visual Studio 中的 NuGet 软件包管理器安装 IronPDF,或直接将软件包添加到项目文件中。另一种方法是从 官方网站 并手动将其添加到项目中。 每种方法都提供了将 IronPDF 集成到您的 C# 项目中以实现 PDF 相关功能的直接方法。

安装 iTextSharp 库

安装 iTextSharp 使用 Visual Studio 中的软件包管理器控制台,可以按照以下步骤操作:

  1. 在 Visual Studio 中,转到工具 -> NuGet 包管理器 -> 包管理器控制台。

  2. 使用以下命令安装 iTextSharp NuGet 软件包:
    Install-Package itext7

这将下载 iTextSharp 软件包及其依赖项,并将其安装到项目中。 安装完成后,你就可以开始在 C# 项目中使用 iTextSharp 处理 PDF 了。

在 C# 中分割 PDF 文档;使用 IronPDF

我们可以使用 IronPDF 将一个 PDF 文件分割成多个 PDF 文件。 它提供了实现这一目标的简单方法。 以下代码将 PDF 源文件作为输入,并将其分割为多个 PDF 文件。

static void Main(string [] args)
 {
    string file = "input.pdf"'
    // Call the SplitPdf method to split the PDF
    SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
 } 
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
 {
     PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
     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);
        string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
        newSplitPDF.SaveAs(name);
        firstPage = lastPage + 1;
        lastPage += totalPageInOneFile;
     }
 }
static void Main(string [] args)
 {
    string file = "input.pdf"'
    // Call the SplitPdf method to split the PDF
    SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles);
 } 
static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles)
 {
     PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath);
     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);
        string name = $@"{outputFolder}\SplitPDF_IronPDF_{i}.pdf";
        newSplitPDF.SaveAs(name);
        firstPage = lastPage + 1;
        lastPage += totalPageInOneFile;
     }
 }
Shared Sub Main(ByVal args() As String)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'	string file = "input.pdf"' SplitPdfUsingIronPDF(file, "output_split", NumberOfSplitFiles); } static void SplitPdfUsingIronPDF(string inputPdfPath, string outputFolder, int NumberOfSplitFiles) { PdfDocument sourceFile = PdfDocument.FromFile(inputPdfPath); int firstPage = 1; int lastPage = 2; int totalPageInOneFile = sourceFile.PageCount / NumberOfSplitFiles; for(int i = 1; i <= NumberOfSplitFiles; i++) { PdfDocument newSplitPDF = sourceFile.CopyPages(firstPage,lastPage); string name = string.Format("{0}\SplitPDF_IronPDF_{1}.pdf", outputFolder, i); newSplitPDF.SaveAs(name); firstPage = lastPage + 1; lastPage += totalPageInOneFile; } }
VB   C#

代码解释

这段代码的目的是使用 IronPDF 库将给定的 PDF 文件分割成多个较小的 PDF 文件。 定义 SplitPdfUsingIronPDF 方法就是为了实现这一功能。

方法参数

  1. 输入 PDF 路径:表示输入 PDF 文件路径的字符串 (例如,"input.pdf").

  2. 输出文件夹表示输出文件夹的字符串,分割后的 PDF 文件将保存在该文件夹中 (例如,"输出分割").

  3. 分割文件数:一个整数,表示原始 PDF 将被分割成多少个较小的 PDF 文件。

拆分过程

在 SplitPdfUsingIronPDF 方法内部:

  1. 通过从指定的 inputPdfPath 加载 PDF,创建名为 sourceFile 的 PdfDocument 对象。

  2. 两个整数变量 firstPage 和 lastPage 已初始化。 这些表示分割的页面范围。

  3. totalPageInOneFile 的计算方法是将源 PDF 的总页数除以指定的 NumberOfSplitFile。

  4. 循环迭代从 1 到 NumberOfSplitFiles:

  5. 创建名为 newSplitPDF 的新 PdfDocument 对象。

  6. 从 firstPage 到 lastPage 的页面 (包容性) 从源文件复制到 newSplitPDF。

  7. 生成的较小 PDF 保存为 "SplitPDF/_IronPDF/_1.pdf "这样的文件名。 (第一次分割) 在指定的输出文件夹中。

  8. firstPage 和 lastPage 值将在下一次迭代中更新。

备注

应将 "input.pdf "替换为输入 PDF 文件的实际路径。确保 IronPDF 库已正确安装并在项目中引用。

输出文件创建为

iTextSharp 和 IronPDF 在 C# 中分割 PDF 的比较:图 3 - 创建的输出文件

在 C# 中分割 PDF;使用 iTextSharp

现在,我们将使用 iTextSharp 将 PDF 文档分割成多个 PDF 文件。 以下代码将源文件作为输入,并将 PDF 文档分割成多个较小的文件。

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;
     // 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))
         {
             int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
             int firstPage = 1;
             int lastPage = totalPageInOneFile; //  int pagenumber
             for (int i = 1; i <= NumberOfSplitFiles; i++)
             {
                 string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
                 using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
                 {
                     //pdfDocument.get
                     doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
                 }
                 firstPage = lastPage + 1;
                 lastPage += totalPageInOneFile;
             }
         }
     }
 }
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;
     // 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))
         {
             int totalPageInOneFile = doc.GetNumberOfPages() / NumberOfSplitFiles;
             int firstPage = 1;
             int lastPage = totalPageInOneFile; //  int pagenumber
             for (int i = 1; i <= NumberOfSplitFiles; i++)
             {
                 string filename = $@"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf";
                 using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename))) // create output file
                 {
                     //pdfDocument.get
                     doc.CopyPagesTo(firstPage, lastPage, pdfDocument);
                 }
                 firstPage = lastPage + 1;
                 lastPage += totalPageInOneFile;
             }
         }
     }
 }
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
	 ' Call the SplitPdf method to split the PDF
	 SplitPdfUsingiTextSharp(inputPath, outputPath, NumberOfSplitFiles)
End Sub
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)
'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 '  int pagenumber
			 For i As Integer = 1 To NumberOfSplitFiles
				 Dim filename As String = $"{outputFolder}\SplitPDF_iTextSharp_{i}.pdf"
				 Using pdfDocument As New PdfDocument(New PdfWriter(filename)) ' create output file
					 'pdfDocument.get
					 doc.CopyPagesTo(firstPage, lastPage, pdfDocument)
				 End Using
				 firstPage = lastPage + 1
				 lastPage += totalPageInOneFile
			 Next i
		 End Using
	 End Using
End Sub
VB   C#

代码解释

上述代码演示了如何使用 iTextSharp 将大型 PDF 文件分割成小块。每个较小的 PDF 文档将包含原始文档的一部分。

使用 iTextSharp 分割 PDF

  1. 主要功能封装在 SplitPdfUsingiTextSharp 方法中。

  2. 上述方法需要三个参数:inputPdfPath、outputFolder 和 NumberOfSplitFiles。

使用 PdfReader 和 PdfDocument

在 SplitPdfUsingiTextSharp 方法内部:

  1. 通过从指定的 inputPdfPath 加载 PDF,创建名为 Reader 的 PdfReader 对象。

  2. 使用阅读器初始化名为 DOC 的 PdfDocument 对象。

  3. 源 PDF 中的总页数除以 NumberOfSplitFiles,即可确定每个较小的 PDF 应包含多少页。

拆分过程

循环迭代从 1 到 NumberOfSplitFiles:

  1. 创建一个新的较小的 PDF 文件,文件名为 "SplitPDF_iTextSharp_1.pdf"。 (第一次分割) 在指定的输出文件夹中。

  2. 在这份新的 PDF 文档中 (pdfDocument), 页面是从原始文件中复制的:

  3. firstPage 至 lastPage (包容性) 复制。

  4. 较小的 PDF 已保存。

  5. firstPage 和 lastPage 值将在下一次迭代中更新。

备注

确保 iTextSharp 库已正确安装并在项目中引用。 将 "input.pdf "替换为输入 PDF 文件的实际路径。

iTextSharp 和 IronPDF 在 C# 中分割 PDF 的比较:图 4 - 创建的输出文件

比较

为了比较使用 iTextSharp 和 IronPDF 的两种拆分方法,让我们根据几个因素对它们进行评估:

  1. 易于使用:

    • iTextSharp: iTextSharp 方法包括创建一个 PdfReader、PdfDocument 和 PdfWriter。 它可以计算页面范围,并将页面复制到新文档中。 这种方法需要了解 iTextSharp API。

    • IronPDF: IronPDF 方法包括从源文件创建一个 PdfDocument,复制页面并将其保存到一个新文件中。它的 API 更为简单明了。
  2. 代码可读性:

    • iTextSharp: 代码涉及更多步骤,因此稍长,阅读起来可能更复杂。

    • IronPDF: 由于减少了步骤和方法调用,代码更加简洁,可读性更高。
  3. 性能:

    • iTextSharp: 重复创建和处理 PdfDocument 实例可能会影响性能。

    • IronPDF: 该方法涉及的步骤更少,由于可以高效复制页面,因此性能更好。
  4. 内存使用量:

    • iTextSharp: 创建多个 PdfDocument 实例将消耗更多内存。

    • IronPDF: 该方法由于简化了方法,因此更节省内存。

结论

总之,iTextSharp 和 IronPDF 都为用 C# 来分割 PDF 文件提供了强大的解决方案,各自都有自己的优势。 IronPDF 因其简单性、可读性以及更直接的方法可能带来的更好性能而脱颖而出。

在多功能性和易用性之间寻求平衡的开发人员可能会发现 IronPDF 是一个令人信服的选择。此外,IronPDF 还提供了以下功能 免费试用. 最终,如何在 iTextSharp 和 IronPDF 之间做出选择取决于各个项目的要求和偏好的开发风格。

< 前一页
PDFsharp 与 iTextSharp(C# PDF 库比较)
下一步 >
IronPDF 与 PDFSharpCore 的比较

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >