翻譯|行業資訊|編輯:胡濤|2024-05-14 10:08:49.777|閱讀 79 次
概述:本文將向您介紹如何在Spire.PDF for .NET中,將多個 PDF 文件中的選定頁面合并為一個,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
使用 Spire.PDF,您不僅可以將多個 PDF 文件合并為一個文件,還可以從源文件中選擇特定頁面并將它們合并為一個 PDF 文檔。以下代碼片段演示了相同的內容。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發人員可以新建一個 PDF 文檔或者對現有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
步驟1:獲取PDF文件路徑并存儲在字符串數組中。
string[] files = { "Sample1.pdf", "Sample2.pdf", "Sample3.pdf" };
步驟2:將每個PDF文檔加載到PdfDocument對象中,并將所有這些對象存儲在PdfDocument數組中。
PdfDocument[] docs = new PdfDocument[files.Length]; for (int i = 0; i < files.Length; i++) { docs[i] = new PdfDocument(files[i]); }
步驟 3:創建 PdfDocument 類的實例。
PdfDocument doc = new PdfDocument();
步驟 4:調用 InsertPage(PdfDocument doc, int pageIndex) 方法和 InertPageRange(PdfDocument doc, int startIndex, int endIndex) 方法將選定的頁面插入到新的 PDF 文檔中。
doc.InsertPage(docs[0], 0);doc.InsertPage(docs[0], 0); doc.InsertPage(docs[1], 1); doc.InsertPageRange(docs[2], 2, 5);
第 5 步:保存并啟動文件。
doc.SaveToFile("Result.pdf"); Process.Start("Result.pdf");
效果截圖:
完整代碼:
[C#]
using Spire.Pdf; using System.Diagnostics; namespace MergeSelectedPages { class Program { static void Main(string[] args) { string[] files = { "Sample1.pdf", "Sample2.pdf", "Sample3.pdf" }; PdfDocument[] docs = new PdfDocument[files.Length]; //open pdf documents for (int i = 0; i < files.Length; i++) { docs[i] = new PdfDocument(files[i]); } //create a new pdf document and insert selected pages PdfDocument doc = new PdfDocument(); doc.InsertPage(docs[0], 0); doc.InsertPage(docs[1], 1); doc.InsertPageRange(docs[2], 2, 5); doc.SaveToFile("Result.pdf"); Process.Start("Result.pdf"); } } }
[VB.NET]
Imports Spire.Pdf Imports System.Diagnostics Namespace MergeSelectedPages Class Program Private Shared Sub Main(args As String()) Dim files As String() = {"Sample1.pdf", "Sample2.pdf", "Sample3.pdf"} Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {} 'open pdf documents For i As Integer = 0 To files.Length - 1 docs(i) = New PdfDocument(files(i)) Next 'create a new pdf document and insert selected pages Dim doc As New PdfDocument() doc.InsertPage(docs(0), 0) doc.InsertPage(docs(1), 1) doc.InsertPageRange(docs(2), 2, 5) doc.SaveToFile("Result.pdf") Process.Start("Result.pdf") End Sub End Class End Namespace
以上便是如何將多個 PDF 文件中的選定頁面合并為一個,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn