如何合併或分割PDF文件於C# | IronPDF教程

如何使用IronPDF在 C# 中 PDF合併或PDF分割

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF使 C# 開發人員能夠使用簡單的方法(例如 Merge() 用於合併,CopyPages() 用於拆分)將多個 PDF 文件合併到一個文件中,或將 PDF 文件簡化為單獨的文件,從而拆分.NET應用程式中的管理文件。

在各種情況下,將多個 PDF 文件合併成一個文件非常有用。 例如,您可以將簡歷等類似文件合併到一個文件中,而不是共享多個文件。 本文將指導您如何使用 C# 合併多個 PDF 檔案。 IronPDF透過 C# 應用程式中直覺的方法調用,簡化了 PDF 的分割和合併。 下面,我們將帶您了解所有頁面操作功能。

快速入門:使用IronPDF合併PDF(合併PDF)

使用IronPDF將多個 PDF 文件合併成一個文件。 只需幾行程式碼,開發人員即可將 PDF 合併功能整合到他們的 C# 應用程式中。 本快速指南示範如何使用IronPDF庫的 Merge 方法合併 PDF,從而簡化文件管理任務。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/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 輸出時,這種方法特別有用。

: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,然後再進行合併。 CSS 屬性確保每個部分都在新頁面上開始。

合併後的PDF檔案是什麼樣子的?

這是程式碼產生的檔案:

何時應該使用PDF合併?

PDF合併在企業環境中尤其有價值,因為文件整合可以提高工作流程效率。 常見應用包括:

-報告產生:使用PDF 報告產生功能,將多個部門報告合併成全面的執行摘要。 -發票處理:將客戶發票與支援文件合併,以簡化計費工作流程 -法律文件:將合約、附件和簽名合併成一份法律文件 -教育資料:將課程資料、作業和資源整合到綜合學習指南中

常見的合併場景有哪些?

除了基本的文件合併功能外, IronPDF還支援進階合併場景:

1.批量處理:使用循環和集合以程式設計方式合併數百個 PDF 文件
2.條件合併:根據業務邏輯或元資料條件合併 PDF 文件
3.模板整合:將動態內容與預先設計的 PDF 範本合併
4.跨格式合併:合併不同來源(例如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")
$vbLabelText   $csharpLabel

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

為什麼要將頁面合併成網格?

將頁面組合成網格佈局有多種用途:

-文件預覽:建立多頁文件的縮圖,以便快速進行視覺掃描
-對比表:並排顯示多個版本,方便比較。
-列印最佳化:透過將多頁內容列印在一張紙上來減少紙張用量
-簡報資料:建立每頁包含多張投影片的講義

CombinePages 的參數是什麼?

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

  1. pageWidth :網格內各頁的寬度,單位為毫米
    2.頁面高度:網格內各頁面的高度(以毫米為單位)
    3.行數:網格佈局中的水平行數
    4.列數:網格佈局中的垂直列數

如需自訂頁面尺寸,請參閱我們的自訂紙張尺寸配置指南。


如何在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

Pages2to3.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

本範例展示如何透過結合分割操作和合併功能來建立自訂文件彙編,非常適合建立高階主管簡報或自訂報告。

準備好要看看你還能做什麼了嗎? 請造訪我們的教學頁面:整理 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-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 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 18,135,201 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 變成 PDF。