在 C# 中如何使用 IronPDF 合併或拆分 PDF
IronPDF 讓 C# 開發人員能夠將多個 PDF 檔案合併成單一文件,或使用類似 Merge() 的簡單方法來合併以及 CopyPages() 來分割 PDF,從而簡化 .NET 應用中的文件管理。
將多個 PDF 檔案合併為一個檔案在多種情況下非常有用。 例如,您可以將類似的文件(如履歷)合併為一個文件,而不是分享多個文件。 本文將指導您如何使用 C# 合併多個 PDF 檔案。 IronPDF 通過在您的 C# 應用中直觀的方法調用,簡化了 PDF 的分割和合併。 以下我們將向您介紹所有頁面操作功能。
快速入門:使用 IronPDF 合併 PDF
using IronPDF 將多個 PDF 檔案合併為單一文件。 通過幾行程式碼,開發人員可以將 PDF 合併功能整合到他們的 C# 應用中。 本快速指南展示如何使用 IronPDF 程式庫的 Merge 方法合併 PDF,簡化文件管理任務。
最小工作流程 (5步)
- 下載 IronPDF 程式庫來操作 PDF 文件
- 載入現有文件或從 HTML 字串、文件或 URL 建立 PDF
- 使用
Merge方法在 C# 中合併兩個 PDF 文件 - 利用
CopyPage和CopyPages方法按頁面拆分 PDF 文件 - 將 PDF 文件儲存到所需位置
如何在 C# 中合併 PDF?
在以下演示中,我們將初始化兩個兩頁的 HTML 字串,使用 IronPDF 將它們渲染為單獨的 PDF,然後合併它們。 此方法特別有用於您需要將 HTML 轉換為 PDF並將多個 HTML 文件合併為單一 PDF 輸出。
: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")
上面的程式碼展示了如何有效地使用分頁符號來建立多頁 PDF,然後進行合併。 page-break-after: always CSS 屬性確保每個部分都在新的一頁開始。
合併的 PDF 會是什麼樣子?
這是程式碼生成的文件:
我應該什麼時候使用 PDF 合併?
PDF 合併在企業環境中特別有價值,因為文件整合提高了工作流程效率。 常見應用包括:
- 報告生成:使用PDF 報告生成將多個部門報告合併為全面的高管摘要
- 發票處理:將客戶發票與支持文件合併以簡化計費流程
- 法律文件:將合同、副本和簽名合併為單一的法律文件
- 教育材料:將課程資料、作業和資源合併為全面的學習指南
常見的合併場景有哪些?
除了基本的文件合併,IronPDF 還支持高級合併場景:
- 批次處理:通過迴圈和集合編程合併數百個 PDF
- 條件合併:根據業務邏輯或元資料標準合併 PDF
- 模板整合:將動態內容與預先設計的 PDF 模板合併
- 跨格式合併:合併來自不同來源的 PDF,如HTML 字串、URL或圖片
我如何合併 PDF 頁面?
using 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")
合併的頁面會是怎樣的?
為什麼要將頁面合併為網格?
將頁面合併為網格佈局有多種用途:
- 文件預覽:建立多頁文件的縮略圖視圖,以便快速視覺掃描
- 比較表:並排顯示多個版本,方便比較
- 列印優化:通過將多頁放在一張紙上來減少紙張使用
- 演示材料:建立每頁包含多個幻燈片的講義
CombinePages 的參數有哪些?
CombinePages 方法接受四個基本參數:
pageWidth:網格中單獨頁面的寬度(毫米)pageHeight:網格中單獨頁面的高度(毫米)rows:網格佈局的水平行數columns:網格佈局的垂直列數
如需自定義頁面大小,請參閱我們的自訂紙張大小配置指南。
我如何在 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")
此程式碼會保存兩個文件:
Page1Only.pdf(僅第一頁)Pages2to3.pdf(第二到第三頁)
拆分的 PDF 會是什麼樣子?
這是兩個生成的文件:
Page1Only.pdf
Pages2to3.pdf
我應該什麼時候拆分 PDF?
PDF 拆分在文件管理中提供了許多優勢:
- 選擇性分發:僅與特定利益關係人分享相關頁面
- 文件大小管理:將大型 PDF 拆分為可管理的塊,以作為電子郵件附件
- 章節提取:從書籍或手冊中提取單獨章節
- 表單處理:將填寫完成的表單與說明頁分開
- 檔案組織:按日期、部門或類別拆分文件
如需更高級的頁面操作,請探索我們關於新增、複製和刪除頁面的指南。
CopyPage 和 CopyPages 之間有什麼不同?
了解這些方法之間的區別對於高效的 PDF 操作至關重要:
CopyPage(int pageIndex):提取指定索引(從零開始)的一頁CopyPages(int startIndex, int endIndex):提取一段頁範圍(包含在內)
這是一個展示兩種方法的高級範例:
:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-5.cs
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")
此範例展示如何通過將拆分操作與合併功能結合來建立自定義文件集合,非常適合建立高管簡報或定制報告。
準備好看看您還能做什麼嗎? 查看我們的教程頁面:組織 PDF,其中包含全面的 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提供CopyPage()和CopyPages()方法來拆分PDF。這些方法允許您從現有的PDF文件中提取單獨的頁面或頁面範圍,並將它們保存為單獨的文件,使編程拆分大型PDF更加容易。
可以將HTML內容合併到PDF中嗎?
當然可以!IronPDF允許您首先使用其HTML到PDF渲染功能將HTML字串轉換為PDF,然後再合併這些生成的PDF。當需要將多個HTML文件或報告合併成一個PDF輸出時,此功能特別有用。
在企業應用中,PDF合併的常見使用案例是什麼?
IronPDF合併功能常用於報告生成(結合部門報告)、發票處理(合併發票與支持文件)和法律文件編輯。這些功能幫助精簡.NET應用中的文件管理工作流程。
在從HTML合併PDF時,我如何確保正確的分頁?
當使用IronPDF將HTML轉換為PDF後再合併時,您可以使用CSS屬性'page-break-after: always'來確保每節都在新頁面開始。這讓您能夠精確控制合併的PDF文件的頁面佈局。

