如何在 C# 中合并或拆分 PDF 文件 | IronPDF 教程

如何使用 IronPDF 在 C# 中合并或拆分 PDF。

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 使 C# 开发人员能够使用简单的方法(如用于合并的 Merge() 和用于分割的 CopyPage()/CopyPages() )将多个 PDF 文件合并为一个文档或将 PDF 分割为单独的文件,从而简化 .NET 应用程序中的文档管理。

将多个PDF文件合并为一个在各种情况下都非常有用。 例如,您可以将类似的文档(如简历)合并成一个文件,而不是分享多个文件。 本文将指导您使用C#合并多个PDF文件的过程。 IronPDF通过在您的C#应用程序中进行直观的方法调用,简化了PDF的拆分和合并。 下面,我们将引导您了解所有的页面操作功能。

快速开始:使用IronPDF合并PDFs

使用 IronPDF 将多个 PDF 文件合并为一个文档。 只需几行代码,开发人员就能将 PDF 合并功能集成到他们的 C# 应用程序中。 本快速指南演示了如何使用 IronPDF 库的 Merge 方法来合并 PDF 文件,从而简化文档管理任务。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronPDF

    PM > Install-Package IronPdf

  2. 复制并运行这段代码。

    IronPdf.PdfDocument
        .Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf"))
        .SaveAs("merged.pdf");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronPDF,免费试用!
    arrow pointer


<! -- 待办事项:在此处添加图片 --> <! --介绍实现的示意图 --> <!--说明:说明代码概念的图表或截图 -->

如何在 C# 中合并 PDF?

在下面的演示中,我们将初始化两个两页的 HTML 字符串,用 IronPDF 将其渲染为单独的 PDF,然后进行合并。 当您需要将 HTML 转换为 PDF 并将多个 HTML 文档合并为一个 PDF 输出时,这种方法尤其有用。

<! -- 待办事项:在此处添加图片 --> <! -- 合并 pdfs 示例实施示意图 --> --> <!--说明:说明代码概念的图表或截图 -->

:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-merge.cs
using IronPdf;

// Two paged PDF
const string html_a =
    @"<p> [PDF_A] </p>
    <p> [PDF_A] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_A] 2nd Page</p>";

// Two paged PDF
const string html_b =
    @"<p> [PDF_B] </p>
    <p> [PDF_B] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_B] 2nd Page</p>";

var renderer = new ChromePdfRenderer();

var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);

// Four paged PDF
var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
merged.SaveAs("Merged.pdf");
Imports IronPdf

' Two paged PDF
Private Const html_a As String = "<p> [PDF_A] </p>
    <p> [PDF_A] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_A] 2nd Page</p>"

' Two paged PDF
Private Const html_b As String = "<p> [PDF_B] </p>
    <p> [PDF_B] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_B] 2nd Page</p>"

Private renderer = New ChromePdfRenderer()

Private pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
Private pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)

' Four paged PDF
Private merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b)
merged.SaveAs("Merged.pdf")
$vbLabelText   $csharpLabel

上面的代码演示了如何在合并前有效使用 分页符 来创建多页 PDF。 page-break-after: always CSS 属性可确保每个部分都从新页面开始。

合并后的 PDF 看起来像什么?

这是代码生成的文件:

何时应使用 PDF 合并?

PDF 合并在企业环境中尤为重要,因为文档合并可以提高工作流程效率。 常见应用包括

  • 报告生成:使用 PDF 报告生成将多个部门报告合并为全面的执行摘要
  • 发票处理:将客户发票与支持文档合并,简化计费工作流
  • 法律文档:将合同、附录和签名合并为单一法律文件
  • 教学材料:将课程材料、作业和资源合并为综合学习指南

常见的合并场景有哪些?

除了基本的文件组合外,IronPDF 还支持高级合并方案:

1.批量处理:使用循环和集合以编程方式合并数以百计的 PDF 文件 2.条件合并:根据业务逻辑或元数据标准合并 PDF 3.模板集成:将动态内容与预先设计的 PDF 模板合并 4.跨格式合并:合并从不同来源创建的 PDF,如 HTML字符串URL图像


如何合并 PDF 页面?

使用CombinePages方法将多个PDF页面合并为单个页面。 该方法需要宽度、高度、行数和列数。 该功能对于创建缩略图、联系表或多页预览特别有用。

:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-combine.cs
using IronPdf;

// Load an existing PDF document from a file.
PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");

// Combine pages of the loaded PDF into a grid with specified dimensions.
// The parameters for CombinePages are the width and height of each page
// in millimeters followed by the number of rows and columns to create the grid.
int pageWidth = 250;  // Width of each page in the grid
int pageHeight = 250; // Height of each page in the grid
int rows = 2;         // Number of rows in the grid
int columns = 2;      // Number of columns in the grid

// Combine the pages of the PDF document into a single page with specified dimensions.
PdfDocument combinedPages = pdf.CombinePages(pageWidth, pageHeight, rows, columns);

// Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf");
Imports IronPdf

' Load an existing PDF document from a file.
Private pdf As PdfDocument = PdfDocument.FromFile("Merged.pdf")

' Combine pages of the loaded PDF into a grid with specified dimensions.
' The parameters for CombinePages are the width and height of each page
' in millimeters followed by the number of rows and columns to create the grid.
Private pageWidth As Integer = 250 ' Width of each page in the grid
Private pageHeight As Integer = 250 ' Height of each page in the grid
Private rows As Integer = 2 ' Number of rows in the grid
Private columns As Integer = 2 ' Number of columns in the grid

' Combine the pages of the PDF document into a single page with specified dimensions.
Private combinedPages As PdfDocument = pdf.CombinePages(pageWidth, pageHeight, rows, columns)

' Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf")
$vbLabelText   $csharpLabel

合并后的页面是什么样的?

为什么要将页面合并到网格中?

将页面组合成网格布局有多种用途:

  • 文档预览:创建多页文档的缩略图,实现快速视觉扫描
  • 比较表:并排显示多个版本,便于比较
  • 打印优化:在一张纸上打印多个页面,从而减少纸张使用量
  • 演示材料:创建每页包含多张幻灯片的讲义

CombinePages的参数是什么?

CombinePages 方法接受四个基本参数:

1.pageWidth:网格内的单个页面宽度(以毫米为单位 2.pageHeight:网格中单个页面的高度(以毫米为单位 3.rows:网格布局中的水平行数 4.:网格布局中的垂直列数

关于自定义页面尺寸,请参阅我们的自定义纸张尺寸配置指南。


如何用 C#分割 PDF?

在下面的演示中,我们将拆分之前示例中的多页PDF文档。 PDF 拆分对于提取特定页面、创建文档摘录或将单个部分分发给不同收件人至关重要。

:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-split.cs
using IronPdf;

// We will use the 4-page PDF from the Merge example above:
var pdf = PdfDocument.FromFile("Merged.pdf");

// Takes only the first page into a new PDF
var page1doc = pdf.CopyPage(0);
page1doc.SaveAs("Page1Only.pdf");

// Take the pages 2 & 3 (Note: index starts at 0)
var page23doc = pdf.CopyPages(1, 2);
page23doc.SaveAs("Pages2to3.pdf");
Imports IronPdf

' We will use the 4-page PDF from the Merge example above:
Private pdf = PdfDocument.FromFile("Merged.pdf")

' Takes only the first page into a new PDF
Private page1doc = pdf.CopyPage(0)
page1doc.SaveAs("Page1Only.pdf")

' Take the pages 2 & 3 (Note: index starts at 0)
Dim page23doc = pdf.CopyPages(1, 2)
page23doc.SaveAs("Pages2to3.pdf")
$vbLabelText   $csharpLabel

该代码保存两个文件:

  • Page1Only.pdf(仅第一页)
  • Pages2to3.pdf (第二到第三页)

拆分后的 PDF 看起来像什么?

这是生成的两个文件:

Page1Only.pdf

Pages2to3.pdf

何时拆分 PDF?

PDF 分割在文档管理方面具有众多优势:

  • 选择性分发:只与特定利益相关者共享相关页面
  • 文件大小管理:将大型 PDF 文件分割成易于管理的小块,用于电子邮件附件
  • 章节提取:从书籍或手册中提取单个章节
  • 表单处理:将完成的表单从说明页面中分离出来
  • 存档组织:按日期、部门或类别分割文件

有关更高级的页面操作,请浏览我们的添加、复制和删除页面指南。

CopyPageCopyPages之间有什么区别?

了解这些方法之间的区别对于高效操作 PDF 至关重要:

  • CopyPage(int pageIndex): 提取指定索引处的单个页面(基于零)
  • CopyPages(int startIndex, int endIndex): 包括提取一系列页面

下面是一个高级示例,展示了这两种方法:

using IronPdf;

// Load a PDF document
var sourcePdf = PdfDocument.FromFile("LargeDocument.pdf");

// Extract cover page (first page)
var coverPage = sourcePdf.CopyPage(0);
coverPage.SaveAs("CoverPage.pdf");

// Extract table of contents (pages 2-5)
var tableOfContents = sourcePdf.CopyPages(1, 4);
tableOfContents.SaveAs("TableOfContents.pdf");

// Extract specific chapter (pages 20-35)
var chapter3 = sourcePdf.CopyPages(19, 34);
chapter3.SaveAs("Chapter3.pdf");

// Create a custom selection by merging specific pages
var customSelection = PdfDocument.Merge(
    sourcePdf.CopyPage(0),      // Cover
    sourcePdf.CopyPages(5, 7),  // Executive Summary
    sourcePdf.CopyPage(50)      // Conclusion
);
customSelection.SaveAs("ExecutiveBrief.pdf");
using IronPdf;

// Load a PDF document
var sourcePdf = PdfDocument.FromFile("LargeDocument.pdf");

// Extract cover page (first page)
var coverPage = sourcePdf.CopyPage(0);
coverPage.SaveAs("CoverPage.pdf");

// Extract table of contents (pages 2-5)
var tableOfContents = sourcePdf.CopyPages(1, 4);
tableOfContents.SaveAs("TableOfContents.pdf");

// Extract specific chapter (pages 20-35)
var chapter3 = sourcePdf.CopyPages(19, 34);
chapter3.SaveAs("Chapter3.pdf");

// Create a custom selection by merging specific pages
var customSelection = PdfDocument.Merge(
    sourcePdf.CopyPage(0),      // Cover
    sourcePdf.CopyPages(5, 7),  // Executive Summary
    sourcePdf.CopyPage(50)      // Conclusion
);
customSelection.SaveAs("ExecutiveBrief.pdf");
Imports IronPdf

' Load a PDF document
Dim sourcePdf = PdfDocument.FromFile("LargeDocument.pdf")

' Extract cover page (first page)
Dim coverPage = sourcePdf.CopyPage(0)
coverPage.SaveAs("CoverPage.pdf")

' Extract table of contents (pages 2-5)
Dim tableOfContents = sourcePdf.CopyPages(1, 4)
tableOfContents.SaveAs("TableOfContents.pdf")

' Extract specific chapter (pages 20-35)
Dim chapter3 = sourcePdf.CopyPages(19, 34)
chapter3.SaveAs("Chapter3.pdf")

' Create a custom selection by merging specific pages
Dim customSelection = PdfDocument.Merge( _
    sourcePdf.CopyPage(0),      ' Cover
    sourcePdf.CopyPages(5, 7),  ' Executive Summary
    sourcePdf.CopyPage(50)      ' Conclusion
)
customSelection.SaveAs("ExecutiveBrief.pdf")
$vbLabelText   $csharpLabel

本示例展示了如何通过将拆分操作与合并功能相结合来创建自定义文档汇编,非常适合创建执行简报或自定义报告。

准备好看看您还能做些什么吗? 点击此处查看我们的教程页面:Organize PDFs 了解全面的 PDF 组织技术,包括元数据管理书签创建和高级页面操作策略。

常见问题解答

如何使用 C# 将多个 PDF 文件合并成一个?

使用 IronPDF,您可以使用简单的 Merge() 方法合并多个 PDF 文件。只需调用 IronPdf.PdfDocument.Merge(),并传入要合并的 PDF 文档,然后用 SaveAs() 保存结果即可。这样,只需几行 C# 代码,就能将多个 PDF 文件合并为一个文档。

合并两个 PDF 文件的最快方法是什么?

最快捷的方法是使用 IronPDF 的单行合并功能:IronPdf.PdfDocument.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")).SaveAs("merged.pdf").这一行代码加载了两个 PDF 文件,并将它们合并为一个。

能否通过提取特定页面来分割 PDF 文件?

是的,IronPDF 提供了用于分割 PDF 的 CopyPage() 和 CopyPages() 方法。通过这些方法,您可以从现有的 PDF 文档中提取单个页面或页面范围,并将其保存为单独的文件,从而轻松地以编程方式分割大型 PDF。

是否可以将 HTML 内容合并到 PDF 中?

当然可以!IronPDF 允许您首先使用其 HTML-to-PDF 渲染功能将 HTML 字符串转换为 PDF,然后再将这些生成的 PDF 合并在一起。当您需要将多个 HTML 文档或报告合并为一个 PDF 输出时,这一点尤其有用。

企业应用程序中 PDF 合并的常见用例有哪些?

IronPDF 的合并功能常用于报告生成(合并部门报告)、发票处理(合并发票与证明文件)以及法律文件汇编。这些功能有助于简化 .NET 应用程序中的文档管理工作流程。

从 HTML 合并 PDF 时,如何确保正确的分页符?

在合并前使用 IronPDF 将 HTML 转换为 PDF 时,您可以使用 CSS 属性 "page-break-after: always "来确保每个部分都从新页面开始。这样,您就可以精确控制合并后 PDF 文档的页面布局。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 17,386,124 | 版本: 2026.2 刚刚发布