如何在C#中使用IronPDF合并或拆分PDF
IronPDF使C#开发人员能够合并多个PDF文件为一个文档,或使用简单的方法如Merge()进行合并,CopyPage()/CopyPages()进行分割,将PDF拆分为独立的文件,从而简化.NET应用程序中的文档管理。
将多个PDF文件合并为一个在各种情况下都非常有用。 例如,您可以将类似的文档(如简历)合并成一个文件,而不是分享多个文件。 本文将指导您使用C#合并多个PDF文件的过程。 IronPDF通过在您的C#应用程序中进行直观的方法调用,简化了PDF的拆分和合并。 下面,我们将引导您了解所有的页面操作功能。
快速入门:使用IronPDF合并 PDF使用 IronPDF 将多个 PDF 文件合并为一个文档。 只需几行代码,开发人员就能将 PDF 合并功能集成到他们的 C# 应用程序中。 本快速指南演示了如何使用IronPDF库的Merge方法来合并PDF,从而简化文档管理任务。
-
1使用 IronPDF 安装 NuGet Package Manager
-
2复制并运行这段代码。
IronPdf.PdfDocument .Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")) .SaveAs("merged.pdf");IronPdf.PdfDocument.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")).SaveAs("merged.pdf")IronPdf.PdfDocument.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")).SaveAs("merged.pdf") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronPDF
最小工作流程(5 个步骤)

- 下载IronPDF库以进行PDF文档操作
- 从 HTML 字符串、文件或 URL 加载现有的或创建 PDF
- 使用
Merge方法在 C# 中合并两个 PDF 文件 - 使用
CopyPage和CopyPages方法按页面分割 PDF 文件 - 将 PDF 文档保存到所需位置
如何在C#中合并PDF?
在下面的演示中,我们将初始化两个两页的 HTML 字符串,用 IronPDF 将其渲染为单独的 PDF,然后进行合并。 这种方法特别有用,当需要将HTML转换为PDF并将多个HTML文档合并为一个PDF输出时。
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");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")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")上述代码演示了如何有效使用分页符来创建多页PDF,然后合并。 page-break-after: always CSS属性确保每个部分都在新页面开始。
合并后的PDF是什么样的?
这是代码生成的文件:
我应该什么时候使用PDF合并功能?
PDF 合并在企业环境中尤为重要,因为文档合并可以提高工作流程效率。 常见应用包括
- 报告生成: 使用PDF报告生成将多个部门报告合并为详尽的执行摘要
- 发票处理: 将客户发票与支持文档合并以简化账单流程
- 法律文件: 将合同、附件和签名整合为单一法律文件
- 教育材料: 将课程材料、作业和资源合并为全面的学习指南
常见的合并场景是什么?
除了基本的文件组合外,IronPDF 还支持高级合并方案:
- 批量处理: 使用循环和集合以编程方式合并数百个PDF
- 条件合并: 基于业务逻辑或元数据标准合并PDF
- 模板集成: 将动态内容与预设计的PDF模板合并
- 跨格式合并: 合并从不同来源创建的PDF,如HTML字符串、URLs,或图像
我如何合并PDF页面?
使用CombinePages方法将多个PDF页面合并为一个页面。 该方法需要宽度、高度、行数和列数。 该功能对于创建缩略图、联系表或多页预览特别有用。
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");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")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")合并的页面是什么样的?
为什么要将页面合并到网格中?
将页面组合成网格布局有多种用途:
- 文档预览: 创建多页文档的缩略图以便快速视觉扫描
- 比较表: 将多个版本并排显示以便于比较
- 打印优化: 通过在一张纸上放置多页来减少纸张使用
- 演示材料: 创建每页包含多个幻灯片的讲义
CombinePages的参数是什么?
CombinePages方法接受四个基本参数:
pageWidth: 网格内单个页面的宽度(以毫米为单位)pageHeight: 网格内单个页面的高度(以毫米为单位)rows: 网格布局中的水平行数columns: 网格布局中的垂直列数
对于自定义页面大小,请参阅我们的自定义纸张大小配置指南。
如何在C#中拆分PDF?
在下面的演示中,我们将拆分之前示例中的多页PDF文档。 PDF 拆分对于提取特定页面、创建文档摘录或将单个部分分发给不同收件人至关重要。
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");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")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")该代码保存两个文件:
Page1Only.pdf(仅第一页)Pages2to3.pdf(第二至第三页)
拆分后的PDF是什么样的?
这是生成的两个文件:
Page1Only.pdf
Pages2to3.pdf
我应该什么时候拆分PDF?
PDF 分割在文档管理方面具有众多优势:
- 选择性分发: 仅与特定利益相关者分享相关页面
- 文件大小管理: 将大型PDF分割为可管理的块用于电子邮件附件
- 章节提取: 从书籍或手册中提取单独章节
- 表单处理: 将完成的表单与说明页分开
- 档案组织: 按日期、部门或类别拆分文档
有关更高级的页面操作,请查看我们的添加、复制和删除页面指南。
CopyPage和CopyPages之间有什么区别?
了解这些方法之间的区别对于高效操作 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(new[] {
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(new[] {
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(New PdfDocument() {
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(New PdfDocument() {
sourcePdf.CopyPage(0), ' Cover
sourcePdf.CopyPages(5, 7), ' Executive Summary
sourcePdf.CopyPage(50) ' Conclusion
})
customSelection.SaveAs("ExecutiveBrief.pdf")本示例展示了如何通过将拆分操作与合并功能相结合来创建自定义文档汇编,非常适合创建执行简报或自定义报告。
准备好看看您还能做些什么吗? 查看我们的教程页面:组织PDF,获取全面的PDF组织技术,包括元数据管理、书签创建和高级页面操作策略。
常见问题
如何在C#中使用IronPDF合并PDF文件?
IronPDF 提供了一个简单的 Merge() 方法,用于将多个 PDF 文件合并为一个文档。此方法简化了 C# 开发人员的合并过程,使他们能够以最少的代码在其 .NET 应用程序中集成合并功能。
IronPDF中的`CopyPage`和`CopyPages`方法有什么区别?
CopyPage(int pageIndex) 从指定的基于零的索引中提取 PDF 的单个页面,而 CopyPages(int startIndex, int endIndex) 则从 PDF 文档中提取一系列页面。
使用IronPDF合并PDF的一些常见用例是什么?
常见用例包括将部门报告整合为单个执行概要,将发票与支持文档合并以简化流程,以及将合同、附录和签名合并为法律合规的文档。
如何使用IronPDF拆分PDF文档?
在 IronPDF 中分割 PDF 可以使用 CopyPage 和 CopyPages 方法。您可以提取单个页面或整个部分,将它们另存为独立的 PDF 文件,实现灵活的文档管理。
我什么时候应该考虑使用IronPDF的PDF拆分功能?
当需要选择性地分发文档部分、管理电子邮件附件的文件大小、提取特定章节或部分、组织档案或处理单个表单时,PDF拆分功能特别有用。
为什么我可能会在IronPDF中使用`CombinePages`方法?
CombinePages 方法非常适合创建缩略图视图、对比表、优化打印布局或生成每页多个幻灯片的讲义,所有这些都可以自定义网格尺寸。
IronPDF可以处理来自不同来源的PDF合并吗?
是的,IronPDF支持跨格式合并,允许合并由HTML字符串、URL、图像或其他内容源创建的PDF,提供了在多种场景中的灵活集成。
如何使用IronPDF自定义合并页面的布局?
使用 CombinePages 方法时,您可以指定网格布局的宽度、高度、行数和列数。这允许您精确地自定义特定页面排列或视觉准备。
哪个编码示例演示了使用IronPDF合并PDF?
一个示例是:IronPdf.PdfDocument.Merge(IronPdf.PdfDocument.FromFile("file1.pdf"), IronPdf.PdfDocument.FromFile("file2.pdf")).SaveAs("merged.pdf");,它将两个 PDF 文件合并为一个。
在C#中,使用IronPDF进行文档管理有哪些好处?
IronPDF通过提供易于使用的方法来合并和拆分PDF,管理文档布局,确保高效的整合或在各种业务需求中进行分发,从而提升文档管理。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。