如何在C#中合併或拆分PDF文件 | IronPDF教學

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

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

IronPDF 讓 C# 開發人員能夠將多個 PDF 檔案合併成單一文件,或使用類似 Merge() 的簡單方法來合併以及 CopyPages() 來分割 PDF,從而簡化 .NET 應用中的文件管理。

將多個 PDF 檔案合併為一個檔案在多種情況下非常有用。 例如,您可以將類似的文件(如履歷)合併為一個文件,而不是分享多個文件。 本文將指導您如何使用 C# 合併多個 PDF 檔案。 IronPDF 通過在您的 C# 應用中直觀的方法調用,簡化了 PDF 的分割和合併。 以下我們將向您介紹所有頁面操作功能。

快速入門:使用 IronPDF 合併 PDF

using 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,然後進行合併。 page-break-after: always CSS 屬性確保每個部分都在新的一頁開始。

合併的 PDF 會是什麼樣子?

這是程式碼生成的文件:

我應該什麼時候使用 PDF 合併?

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

  • 報告生成:使用PDF 報告生成將多個部門報告合併為全面的高管摘要
  • 發票處理:將客戶發票與支持文件合併以簡化計費流程
  • 法律文件:將合同、副本和簽名合併為單一的法律文件
  • 教育材料:將課程資料、作業和資源合併為全面的學習指南

常見的合併場景有哪些?

除了基本的文件合併,IronPDF 還支持高級合併場景:

  1. 批次處理:通過迴圈和集合編程合併數百個 PDF
  2. 條件合併:根據業務邏輯或元資料標準合併 PDF
  3. 模板整合:將動態內容與預先設計的 PDF 模板合併
  4. 跨格式合併:合併來自不同來源的 PDF,如HTML 字串URL圖片

Icon Quote related to 常見的合併場景有哪些?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 常見的合併場景有哪些?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 常見的合併場景有哪些?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 常見的合併場景有哪些?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 常見的合併場景有哪些?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 常見的合併場景有哪些?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

我如何合併 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")
$vbLabelText   $csharpLabel

合併的頁面會是怎樣的?

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

將頁面合併為網格佈局有多種用途:

  • 文件預覽:建立多頁文件的縮略圖視圖,以便快速視覺掃描
  • 比較表:並排顯示多個版本,方便比較
  • 列印優化:通過將多頁放在一張紙上來減少紙張使用
  • 演示材料:建立每頁包含多個幻燈片的講義

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

Pages2to3.pdf

我應該什麼時候拆分 PDF?

PDF 拆分在文件管理中提供了許多優勢:

  • 選擇性分發:僅與特定利益關係人分享相關頁面
  • 文件大小管理:將大型 PDF 拆分為可管理的塊,以作為電子郵件附件
  • 章節提取:從書籍或手冊中提取單獨章節
  • 表單處理:將填寫完成的表單與說明頁分開
  • 檔案組織:按日期、部門或類別拆分文件

如需更高級的頁面操作,請探索我們關於新增、複製和刪除頁面的指南。

CopyPageCopyPages 之間有什麼不同?

了解這些方法之間的區別對於高效的 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")
$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到PDF渲染功能將HTML字串轉換為PDF,然後再合併這些生成的PDF。當需要將多個HTML文件或報告合併成一個PDF輸出時,此功能特別有用。

在企業應用中,PDF合併的常見使用案例是什麼?

IronPDF合併功能常用於報告生成(結合部門報告)、發票處理(合併發票與支持文件)和法律文件編輯。這些功能幫助精簡.NET應用中的文件管理工作流程。

在從HTML合併PDF時,我如何確保正確的分頁?

當使用IronPDF將HTML轉換為PDF後再合併時,您可以使用CSS屬性'page-break-after: always'來確保每節都在新頁面開始。這讓您能夠精確控制合併的PDF文件的頁面佈局。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 19,936,792 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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