跳過到頁腳內容
使用IRONPDF

您如何在C# .NET中合併PDF文件?

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 1 - 使用 IronPDF 合併 PDF

將多個 PDF 檔案合併為單一文件,是商業應用程式中常見的需求。 無論是生成綜合財務報告、將法律合約打包歸檔,還是彙整多聯發票,透過程式化方式合併 PDF 皆能節省時間並省去手動步驟。 IronPDF 提供直觀的 API,僅需幾行 C# 程式碼即可完成此操作,無需任何外部工具或命令列程式。

本指南涵蓋了所有實際場景:雙檔案合併、多檔案清單合併、使用 MemoryStream 進行記憶體處理,以及合併後操作,如密碼保護和壓縮。

立即開始免費試用,為您的 .NET 應用程式增添 PDF 合併功能。

如何開始使用 IronPDF?

在編寫任何合併程式碼之前,請先從 NuGet 安裝 IronPDF。 此套件支援 .NET Framework 4.6.2 及以上版本,以及 .NET 5/6/7/8/9/10,因此可在控制台應用程式、ASP.NET Core 服務、Azure Functions 及 Windows Forms 專案中運作,無需任何額外的執行階段依賴項。

開啟 NuGet 套件管理員控制台並執行:

Install-Package IronPdf
Install-Package IronPdf
SHELL

或使用 .NET CLI:

dotnet add package IronPdf
dotnet add package IronPdf
SHELL

如需包含授權金鑰申請步驟的詳細設定指南,請參閱 IronPDF 安裝概覽。 安裝套件並套用授權金鑰後,本指南中的所有範例皆可直接執行,無需修改。

如何透過程式碼合併 PDF 文件?

PdfDocument.Merge 方法提供了將兩個 PDF 檔案合併為一個 PDF 的最直接方法。 使用 PdfDocument.FromFile 載入每個來源文檔,將這兩個物件傳遞給 Merge,並儲存結果。

輸入發票一

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 2 - 輸入發票一

輸入發票二

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 3 - 輸入發票二

using IronPdf;

// Load two input PDF files from disk
PdfDocument pdfA = PdfDocument.FromFile("invoice_one.pdf");
PdfDocument pdfB = PdfDocument.FromFile("invoice_two.pdf");

// Merge PDF documents into a single PDF
PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Save the merged document
merged.SaveAs("combined_invoices.pdf");
using IronPdf;

// Load two input PDF files from disk
PdfDocument pdfA = PdfDocument.FromFile("invoice_one.pdf");
PdfDocument pdfB = PdfDocument.FromFile("invoice_two.pdf");

// Merge PDF documents into a single PDF
PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Save the merged document
merged.SaveAs("combined_invoices.pdf");
Imports IronPdf

' Load two input PDF files from disk
Dim pdfA As PdfDocument = PdfDocument.FromFile("invoice_one.pdf")
Dim pdfB As PdfDocument = PdfDocument.FromFile("invoice_two.pdf")

' Merge PDF documents into a single PDF
Dim merged As PdfDocument = PdfDocument.Merge(pdfA, pdfB)

' Save the merged document
merged.SaveAs("combined_invoices.pdf")
$vbLabelText   $csharpLabel

輸出

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 4 - 合併 PDF 文件

Merge 方法接受兩個 PdfDocument 實例,並傳回一個新文檔,其中包含來自兩個來源的所有頁面,並按順序排列。 生成的檔案將保留各輸入檔案的原始格式、圖片、字型及嵌入式內容。 完成文件合併後,您可以將其儲存至磁碟、透過網頁端點傳回,或在交付給使用者前添加頁首與頁尾

請注意,PdfDocument.FromFile 既可以用於文件路徑,也可以用於 URI 字串,因此儲存在雲端 Blob 儲存中的文件可以直接透過 URL 載入。 合併操作為非破壞性:呼叫後,兩個來源物件將保持不變。

如何一次合併多個 PDF 檔案?

當您需要合併兩個以上的文件時,請將 List<PdfDocument>(或任何 IEnumerable<PdfDocument>)傳遞給重載的 Merge 方法。 此方法可從三個檔案擴展至數百個檔案,且無需變更您的程式碼結構。

using IronPdf;

// Build a list of quarterly reports
List<PdfDocument> pdfsToMerge = new()
{
    PdfDocument.FromFile("report_q1.pdf"),
    PdfDocument.FromFile("report_q2.pdf"),
    PdfDocument.FromFile("report_q3.pdf"),
    PdfDocument.FromFile("report_q4.pdf")
};

// Combine into one annual report
PdfDocument merged = PdfDocument.Merge(pdfsToMerge);

// Save the final document
merged.SaveAs("annual_report.pdf");
using IronPdf;

// Build a list of quarterly reports
List<PdfDocument> pdfsToMerge = new()
{
    PdfDocument.FromFile("report_q1.pdf"),
    PdfDocument.FromFile("report_q2.pdf"),
    PdfDocument.FromFile("report_q3.pdf"),
    PdfDocument.FromFile("report_q4.pdf")
};

// Combine into one annual report
PdfDocument merged = PdfDocument.Merge(pdfsToMerge);

// Save the final document
merged.SaveAs("annual_report.pdf");
Imports IronPdf

' Build a list of quarterly reports
Dim pdfsToMerge As New List(Of PdfDocument) From {
    PdfDocument.FromFile("report_q1.pdf"),
    PdfDocument.FromFile("report_q2.pdf"),
    PdfDocument.FromFile("report_q3.pdf"),
    PdfDocument.FromFile("report_q4.pdf")
}

' Combine into one annual report
Dim merged As PdfDocument = PdfDocument.Merge(pdfsToMerge)

' Save the final document
merged.SaveAs("annual_report.pdf")
$vbLabelText   $csharpLabel

合併的 PDF 文件

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 5 - 新合併的 PDF 輸出檔案

IronPDF 會依照清單順序處理頁面,因此您的 List<PdfDocument> 中的文件順序直接控制輸出中的頁面順序。 如果在合併後需要重新排序頁面,頁面管理 API可讓您從任何 PdfDocument 複製、移動或刪除單一頁面。

這種模式也適用於執行時期產生的文件。例如,您可以將報表的每個部分轉換為 PDF ,然後合併產生的 PdfDocument 對象,而無需將中間檔案寫入磁碟。 當原始內容源自 WORD 文件時,此原則同樣適用於 DOCX 轉 PDF 的轉換作業

如何使用 MemoryStream 合併 PDF 檔案?

當無法寫入磁碟或寫入速度過慢時,Web 服務和無伺服器函式便需要採用記憶體內處理。 IronPDF 支援從位元組數組載入 PDF 文檔,並透過 @@--CODE-26405--@@ 屬性將合併結果公開為 @@--CODE-26404--@@。

using IronPdf;
using System.IO;

// Load PDF content as byte arrays (e.g., from database or HTTP response)
byte[] firstFileBytes = File.ReadAllBytes("contract_part1.pdf");
byte[] secondFileBytes = File.ReadAllBytes("contract_part2.pdf");

// Create PdfDocument objects from byte arrays
PdfDocument doc1 = new PdfDocument(firstFileBytes);
PdfDocument doc2 = new PdfDocument(secondFileBytes);

// Merge and access the result as a stream
PdfDocument merged = PdfDocument.Merge(doc1, doc2);
MemoryStream outputStream = merged.Stream;

// Optionally save to disk as well
merged.SaveAs("merged_contract.pdf");
using IronPdf;
using System.IO;

// Load PDF content as byte arrays (e.g., from database or HTTP response)
byte[] firstFileBytes = File.ReadAllBytes("contract_part1.pdf");
byte[] secondFileBytes = File.ReadAllBytes("contract_part2.pdf");

// Create PdfDocument objects from byte arrays
PdfDocument doc1 = new PdfDocument(firstFileBytes);
PdfDocument doc2 = new PdfDocument(secondFileBytes);

// Merge and access the result as a stream
PdfDocument merged = PdfDocument.Merge(doc1, doc2);
MemoryStream outputStream = merged.Stream;

// Optionally save to disk as well
merged.SaveAs("merged_contract.pdf");
Imports IronPdf
Imports System.IO

' Load PDF content as byte arrays (e.g., from database or HTTP response)
Dim firstFileBytes As Byte() = File.ReadAllBytes("contract_part1.pdf")
Dim secondFileBytes As Byte() = File.ReadAllBytes("contract_part2.pdf")

' Create PdfDocument objects from byte arrays
Dim doc1 As New PdfDocument(firstFileBytes)
Dim doc2 As New PdfDocument(secondFileBytes)

' Merge and access the result as a stream
Dim merged As PdfDocument = PdfDocument.Merge(doc1, doc2)
Dim outputStream As MemoryStream = merged.Stream

' Optionally save to disk as well
merged.SaveAs("merged_contract.pdf")
$vbLabelText   $csharpLabel

合併合約檔案

.NET PDF Merge Tasks with IronPDF:完整的 C# 指南:圖片 6 - 合併合約 PDF 文件

Stream 屬性傳回一個位於開頭的 MemoryStream 對象,可用於寫入 HTTP 回應或傳遞給其他處理步驟。在 ASP.NET Core 控制器中,您可以直接傳回 File(outputStream, "application/pdf", "merged.pdf") 物件。 不會產生任何暫存檔,這簡化了清理流程,並在高吞吐量情境下減少磁碟負載。

IronPDF 可交替接受字串檔案路徑、位元組陣列及資料流作為輸入來源,因此您可以在同一次合併呼叫中混合使用這些格式。 從資料庫 BLOB 載入的文件,可與從 URL 擷取的文件合併,無需任何中間轉換步驟。

在合併檔案中,書籤與導覽功能如何被保留?

當您合併 PDF 檔案時,IronPDF 會保留每個來源文件的書籤結構。 讀者在合併輸出文件的導覽窗格中,可查看所有合併檔案的完整目錄,讓使用者能輕鬆地在源自不同原始文件的各章節間跳轉。 PDF 規格中的書籤儲存於文件大綱字典中,而 IronPDF 會針對所有合併的來源文件正確重建此結構。

書籤的保留功能會自動執行,無需額外設定。 若原始檔案包含嵌套式書籤(章節與子節),合併後的輸出檔應保留其層級結構。 對於需要統一目錄而非各自獨立書籤樹的合併文件,目錄 API 可讓您在合併後建立自訂的導覽結構。

若原始文件未包含書籤元資料,合併後的輸出內容將不會針對該段落包含大綱條目。 在合規或歸檔環境中交付合併文件之前,請檢查生成的 PdfDocument 上的 OutlineManager 屬性,以驗證哪些書籤已被繼承。 您可以透過程式設計方式重新命名書籤標籤、重新排序項目,或插入新項目,將來自不同來源文件的內容整合至邏輯層級結構中。 若工作流程還需進行 PDF/A 轉換以供長期歸檔,請在合併後執行合規轉換步驟,而非對每個原始檔案分別處理。 一次性轉換最終合併後的文件更為高效,並可避免因需進一步編輯而導致的輸出檔案重複轉換。

合併後,您亦可透過頁碼編號功能為合併後的文件添加頁碼,此功能在輸出文件涵蓋來自多個來源的數十頁內容時特別實用。

如何對合併後的 PDF 檔案套用密碼保護?

合併後,產生的 PdfDocument 公開了 SecuritySettings,用於應用使用者和所有者密碼,限制列印、複製和編輯權限。

using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("part1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("part2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Require a password to open the document
merged.SecuritySettings.UserPassword = "reader123";

// Owner password controls permissions such as printing and editing
merged.SecuritySettings.OwnerPassword = "admin456";

merged.SaveAs("secured_merged.pdf");
using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("part1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("part2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Require a password to open the document
merged.SecuritySettings.UserPassword = "reader123";

// Owner password controls permissions such as printing and editing
merged.SecuritySettings.OwnerPassword = "admin456";

merged.SaveAs("secured_merged.pdf");
Imports IronPdf

Dim pdfA As PdfDocument = PdfDocument.FromFile("part1.pdf")
Dim pdfB As PdfDocument = PdfDocument.FromFile("part2.pdf")

Dim merged As PdfDocument = PdfDocument.Merge(pdfA, pdfB)

' Require a password to open the document
merged.SecuritySettings.UserPassword = "reader123"

' Owner password controls permissions such as printing and editing
merged.SecuritySettings.OwnerPassword = "admin456"

merged.SaveAs("secured_merged.pdf")
$vbLabelText   $csharpLabel

UserPassword 會在讀者查看文件之前發出提示,而 OwnerPassword 則會阻止權限變更。 您可以將密碼保護與列印限制、防複製標記及數位簽章結合使用。 若需完整了解權限選項,PDF 安全性教學指南涵蓋了所有可用設定。

若原始文件本身設有密碼保護,只要您擁有擁有者憑證,IronPDF 便能在合併前解密加密的 PDF 檔案。 這意味著您可以將來自不同供應商或系統的受保護文件合併為單一、重新加密的輸出檔,無需手動解鎖步驟。

如何縮小合併後 PDF 檔案的大小?

合併包含大量圖片的大型文件,可能會產生體積龐大、難以儲存或傳輸的輸出檔案。 IronPDF 的 PDF 壓縮 API 可透過以較低品質設定重新壓縮嵌入的圖片,同時保持文字清晰度,從而顯著減少檔案大小。

using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("report_section1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("report_section2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Compress embedded images to reduce output file size
merged.CompressImages(60); // quality 0–100

merged.SaveAs("compressed_merged.pdf");
using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("report_section1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("report_section2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Compress embedded images to reduce output file size
merged.CompressImages(60); // quality 0–100

merged.SaveAs("compressed_merged.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

CompressImages 的整數參數是一個品質百分比:50 到 75 之間的值通常會將影像密集型文件的檔案大小減少 40-70%,而不會對文字內容造成明顯的劣化。 對於包含向量圖形且僅有少量點陣圖的文件,雖然節省的空間較小,但仍建議將此呼叫納入。

哪些是 PDF 合併的常見使用案例?

PdfDocument.Merge API 適用於生產環境中 .NET 應用程式的各種文件工作流程:

常見的 PDF 合併情境及 IronPDF 對應的解決方案
用例輸入來源建議的翻譯方法
年度財務報告磁碟上的四個季度 PDF 檔案使用 Merge(List<PdfDocument>) 進行清單合併
多部分法律合約基礎合約 + 來自資料庫的修訂條款位元組陣列載入 + 記憶體內合併
發票整合按明細項目渲染的 HTML 發票逐項將 HTML 轉換為 PDF,然後合併清單
自動化報告傳送混合 DOCX 與 PDF 原始檔案DOCX 轉 PDF,然後合併
安全的文件歸檔系統多部門報告清單合併 + SecuritySettings

對於需要在合併前選擇性提取文件的工作流程,拆分 PDF API 可從原始文件中提取特定的頁面範圍。 您可以直接在 PDF 檔案之間複製頁面,這在需要將大型文件中的特定段落顯示於合併後的輸出時非常實用。

IronPDF 亦支援從合併文件中擷取文字與圖片以供後續處理,使其適合作為大型資料處理流程中關鍵的文件組裝步驟。

下一步計劃是什麼?

IronPDF 的 PdfDocument.Merge 方法以最少的程式碼涵蓋了 .NET PDF 合併場景的全部範圍:雙檔案合併、多檔案清單合併、記憶體處理、密碼保護輸出和合併後壓縮。 無論輸入來源類型為何,每種方法皆採用相同且一致的 API。

立即開始免費試用,將這些範例應用於您的專案中。 若需進行生產環境部署,請查看符合您團隊規模與使用情境的授權方案

新增合併功能後,可進一步探索相關文件操作:將 PDF 分割為獨立檔案為多頁輸出添加頁碼,或對合併文件中的所有頁面套用浮水印

!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101 01000101111101010011010101000100000101010010010101000100010101000100010111110101011101001000110 1010101000100100001011111010100000101001001001111010001000101010101010000110101010100101010101011 10101010001010010010010010010000010100110001011111010000100100110001001111101000011010010111111010000110100101110--

常見問題解答

如何在C#中合併兩個PDF檔案?

使用 PdfDocument.FromFile 載入每個文件,然後呼叫 PdfDocument.Merge(pdfA, pdfB) 並使用 merged.SaveAs 保存結果。

如何在 .NET 中合併超過兩個 PDF 文件?

創建一個包含所有來源文件的 List 並將其傳遞給 PdfDocument.Merge(list)。輸出保持列表順序。

您是否可以在不寫入磁碟的情況下在記憶體中合併 PDF?

是的。使用 new PdfDocument(bytes) 從位元組陣列載入文件,合併它們,並透過 merged.Stream 訪問結果以避免任何磁碟寫入。

IronPDF 在合併 PDF 時是否保留書籤?

是的。IronPDF 自動從每個來源文件中將書籤(大綱)結構傳遞到合併輸出中,包括嵌套的書籤層次結構。

如何在 C# 中為合併的 PDF 設置密碼保護?

合併後,設置 merged.SecuritySettings.UserPassword 為讀者密碼,設置 merged.SecuritySettings.OwnerPassword 為權限密碼,然後呼叫 SaveAs

如何減少合併 PDF 的檔案大小?

在保存前使用 merged.CompressImages(quality) 呼叫,設置質量值在 50 到 75 之間。這將重新壓縮嵌入的光柵圖像,通常減少 40-70% 圖像密集文檔的檔案大小。

IronPDF 能合併加密的 PDF 文件嗎?

是的。IronPDF 可以在您擁有擁有者密碼時載入並合併加密的 PDF。使用 PdfDocument.FromFile(path, password) 在合併前解鎖文件。

IronPDF 的合併 API 在 ASP.NET Core 中是否適用?

是的。合併結果可以從任何 ASP.NET Core 控制器動作中作為 File(merged.Stream, "application/pdf", "merged.pdf") 返回。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我