如何使用 IronPDF 在 C# 中合併或分割 PDF 檔案
IronPDF 讓 C# 開發人員能夠透過簡單的程式碼方法(例如用於合併的 Merge(),以及用於分割的 CopyPages()),將多個 PDF 檔案合併為單一文件或分割成獨立檔案,從而簡化 .NET 應用程式中的文件管理流程。
在各種情境下,將多個 PDF 檔案合併為一個檔案往往非常實用。 例如,您可以將類似文件(如履歷)整合為單一檔案,而非分享多個檔案。 本文將引導您了解如何使用 C# 合併多個 PDF 檔案。 IronPDF 透過在您的 C# 應用程式中呼叫直觀的方法,簡化 PDF 分割與合併的流程。 以下,我們將帶您逐步了解所有頁面操作功能。
快速入門:使用 IronPDF 合併 PDF 檔案
使用 IronPDF 將多個 PDF 檔案合併為單一文件。 開發人員只需幾行程式碼,即可將 PDF 合併功能整合至其 C# 應用程式中。 本快速指南將示範如何使用 IronPDF 函式庫的 Merge 方法來合併 PDF 檔案,藉此簡化文件管理任務。
簡化工作流程(5 個步驟)
- 下載 IronPDF 函式庫以進行 PDF 文件處理
- 載入現有 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 合併功能?
在 Enterprise 環境中,PDF 合併功能尤為重要,因為文件整合能有效提升工作流程效率。 常見應用包括:
- 報表生成:利用 PDF 報表生成功能,將多個部門的報表整合為全面的執行摘要
- 發票處理:將客戶發票與相關文件合併,以簡化帳務處理流程
- 法律文件:將合約、補充協議及簽名整合為單一法律文件
- 教學材料:將課程教材、作業及資源整合為全面性的學習指南
常見的合併情境有哪些?
除了基本的檔案合併功能外,IronPDF 還支援進階的合併情境:
- 批次處理:透過迴圈和集合,以程式化方式合併數百個 PDF 檔案
- 條件式合併:根據業務邏輯或元數據條件合併 PDF 檔案
- 範本整合:將動態內容與預先設計的 PDF 範本合併
- 跨格式合併:將來自不同來源(如 HTML 字串、URL 或圖片)所建立的 PDF 檔案進行合併
如何合併 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")
整合後的頁面呈現為何?
為何要將頁面合併為網格?
將頁面組合成網格佈局具有多重用途:
- 文件預覽:為多頁文件建立縮圖檢視,以便快速視覺掃描
- 比較表:將多個版本並列顯示,以便輕鬆比較
- PRINT最佳化:透過將多頁內容排版至單張紙上,以減少用紙量
- 簡報材料:製作每頁包含多張投影片的講義
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):擷取包含起始頁與結束頁在內的一段頁面範圍
以下是一個同時展示兩種方法的進階範例:
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")
此範例展示如何透過結合分割操作與合併功能來建立自訂文件彙編,非常適合用於製作高層簡報或客製化報告。
準備好探索更多可能性了嗎? 請參閱我們的教學頁面:整理 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 輸出時,此功能特別實用。
在 Enterprise 應用程式中,PDF 合併有哪些常見的應用情境?
IronPDF 的合併功能常被用於報表生成(整合部門報表)、發票處理(將發票與佐證文件合併)以及法律文件彙編。這些功能有助於簡化 .NET 應用程式中的文件管理工作流程。
如何在將 HTML 合併為 PDF 時確保適當的分頁?
在合併前使用 IronPDF 將 HTML 轉換為 PDF 時,您可以使用 CSS 屬性 'page-break-after: always',以確保每個區段都從新頁面開始。這讓您能夠精確控制合併後 PDF 文件的版面配置。

