如何使用IronPDF在C#中合併或分割PDF文件
IronPDF使C#開發者能夠通過簡單的方法如Merge()將多個PDF文件合併為一個文件,或使用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或創建新的PDF
- 使用 C# 中的
Merge方法合併兩個 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字串、URL或圖片
我如何合併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
第2至3頁.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組織技術。
常見問題
如何使用IronPDF在C#中合併PDF檔案?
IronPDF提供一個簡單的Merge()方法,用於將多個PDF文件合併為單一文件。此方法簡化了C#開發人員的合併過程,允許他們用最少的代碼將合併功能整合到他們的.NET應用程式中。
IronPDF中的`CopyPage`和`CopyPages`方法有什麼區別?
CopyPage(int pageIndex)從PDF中提取指定零基索引的一頁,而CopyPages(int startIndex, int endIndex)則提取PDF文件的一個頁面範圍。
合併PDF與IronPDF的常見用途有哪些?
常見用途包括將部門報告合併成單一的執行摘要,將發票與支持文件合併以簡化,和將合約、附錄及簽名合併成法律合規的文件。
如何使用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。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。