翻譯|行業資訊|編輯:胡濤|2024-04-08 11:44:36.707|閱讀 96 次
概述:在本文中,您將學習如何使用Spire.PDF for .NET將多個 PDF 文檔合并為一個 PDF 文檔,以及如何使用C# 和 VB.NET將不同 PDF 文檔中的選定頁面合并為一個 PDF 。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
需要合并 PDF 的原因有很多。例如,合并 PDF 文件允許您打印單個文件,而不是為打印機排隊多個文檔,組合相關文件通過減少要搜索和組織的文件數量來簡化管理和存儲多個文檔的過程。在本文中,您將學習如何使用Spire.PDF for .NET將多個 PDF 文檔合并為一個 PDF 文檔,以及如何使用C# 和 VB.NET將不同 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下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。 DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
Spire.PDF for .NET 提供PdfDocument.MergeFiles()方法將多個 PDF 文檔合并為單個文檔。詳細步驟如下。
C#
using System; using Spire.Pdf; namespace MergePDFs { class Program { static void Main(string[] args) { //Get the paths of the documents to be merged String[] files = new String[] { "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"}; //Merge these documents and return an object of PdfDocumentBase PdfDocumentBase doc = PdfDocument.MergeFiles(files); //Save the result to a PDF file doc.Save("output.pdf", FileFormat.PDF); } } }
VB.NET
Imports System Imports Spire.Pdf Namespace MergePDFs Class Program Shared Sub Main(ByVal args() As String) 'Get the paths of the documents to be merged Dim files() As String = New String() {"C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"} 'Merge these documents and return an object of PdfDocumentBase Dim doc As PdfDocumentBase = PdfDocument.MergeFiles(files) 'Save the result to a PDF file doc.Save("output.pdf", FileFormat.PDF) End Sub End Class End Namespace
Spire.PDF for .NET 提供PdfDocument.InsertPage()方法和PdfDocument.InsertPageRange()方法,用于將頁面或頁面范圍從一個 PDF 文檔導入到另一個 PDF 文檔。以下是將不同 PDF 文檔中的選定頁面合并為一個新 PDF 文檔的步驟。
C#
using System; using Spire.Pdf; namespace MergeSelectedPages { class Program { static void Main(string[] args) { //Get the paths of the documents to be merged String[] files = new String[] { "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf", "C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"}; //Create an array of PdfDocument PdfDocument[] docs = new PdfDocument[files.Length]; //Loop through the documents for (int i = 0; i < files.Length; i++) { //Load a specific document docs[i] = new PdfDocument(files[i]); } //Create a PdfDocument object for generating a new PDF document PdfDocument doc = new PdfDocument(); //Insert the selected pages from different documents to the new document doc.InsertPage(docs[0], 0); doc.InsertPageRange(docs[1], 1,3); doc.InsertPage(docs[2], 0); //Save the document to a PDF file doc.SaveToFile("output.pdf"); } } }
VB.NET
Imports System Imports Spire.Pdf Namespace MergeSelectedPages Class Program Shared Sub Main(ByVal args() As String) 'Get the paths of the documents to be merged Dim files() As String = New String() {"C:\\Users\\Administrator\\Desktop\\PDFs\\sample-1.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-2.pdf","C:\\Users\\Administrator\\Desktop\\PDFs\\sample-3.pdf"} 'Create an array of PdfDocument Dim docs() As PdfDocument = New PdfDocument(files.Length) {} 'Loop through the documents Dim i As Integer For i = 0 To files.Length- 1 Step i + 1 'Load a specific document docs(i) = New PdfDocument(files(i)) Next 'Create a PdfDocument object for generating a new PDF document Dim doc As PdfDocument = New PdfDocument() 'Insert the selected pages from different documents to the new document doc.InsertPage(docs(0), 0) doc.InsertPageRange(docs(1), 1,3) doc.InsertPage(docs(2), 0) 'Save the document to a PDF file doc.SaveToFile("output.pdf") End Sub End Class End Namespace
以上便是如何合并 PDF 文件并添加頁碼,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn