如何合併或分割PDF文件於C# | 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 合併 PDF

使用 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 輸出時,此方法尤其有用。

<! --! 說明 merge 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字串URLs,或 圖片


如何合併 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

合併後的頁面是什麼樣子?

Why Combine Pages Into a Grid?

將頁面組合為網格佈局可達到多種目的:

  • 文件預覽:建立多頁文件的縮圖檢視,以便快速視覺掃描
  • 比較表:並排顯示多個版本,方便比較
  • 列印最佳化:在單一頁面上容納多個頁面以減少紙張使用量
  • 簡報材料:以每頁多張幻燈片來製作講義

CombinePages的參數是什麼?

CombinePages 方法接受四個基本參數:

1.pageWidth:網格內以毫米為單位的個別頁面寬度 2.pageHeight:網格內以毫米為單位的個別頁面高度 3.rows:網格佈局中的水平行數量 4.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")
$vbLabelText   $csharpLabel

此程式碼會儲存兩個檔案:

  • Page1Only.pdf(僅第一頁)
  • Pages2to3.pdf (第二頁至第三頁)

分割後的 PDF 是什麼樣子?

產生的兩個文件如下:

Page1Only.pdf

第2至3頁.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 提供 CopyPage() 和 CopyPages() 方法來分割 PDF。這些方法允許您從現有的 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 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 17,386,124 | 版本: 2026.2 剛剛發布