需要合并 PDF 的原因有很多。例如,合并 PDF 文件允許您打印單個(gè)文件,而不是為打印機(jī)排隊(duì)多個(gè)文檔,組合相關(guān)文件通過(guò)減少要搜索和組織的文件數(shù)量來(lái)簡(jiǎn)化管理和存儲(chǔ)多個(gè)文檔的過(guò)程。在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET將多個(gè) PDF 文檔合并為一個(gè) PDF 文檔,以及如何使用C# 和 VB.NET將不同 PDF 文檔中的選定頁(yè)面合并為一個(gè) PDF 。
Spire.PDF for .NET?是一款獨(dú)立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類(lèi)庫(kù),開(kāi)發(fā)人員可以新建一個(gè) PDF 文檔或者對(duì)現(xiàn)有的 PDF 文檔進(jìn)行處理,且無(wú)需安裝 Adobe Acrobat。
E-iceblue?功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴(lài)第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載? ?Spire.PDF for java下載
安裝適用于 .NET 的 Spire.PDF
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目中的引用。 DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.PDF
將多個(gè) PDF 合并為一個(gè) PDF
Spire.PDF for .NET 提供PdfDocument.MergeFiles()方法將多個(gè) PDF 文檔合并為單個(gè)文檔。詳細(xì)步驟如下。
- 獲取要合并的文檔的路徑并將其存儲(chǔ)在字符串?dāng)?shù)組中。
- 調(diào)用PdfDocument.MergeFiles()方法來(lái)合并這些文件。
- 使用PdfDocumentBase.Save()方法將結(jié)果保存到 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
將不同 PDF 的選定頁(yè)面合并為一個(gè) PDF
Spire.PDF for .NET 提供PdfDocument.InsertPage()方法和PdfDocument.InsertPageRange()方法,用于將頁(yè)面或頁(yè)面范圍從一個(gè) PDF 文檔導(dǎo)入到另一個(gè) PDF 文檔。以下是將不同 PDF 文檔中的選定頁(yè)面合并為一個(gè)新 PDF 文檔的步驟。
- 獲取源文檔的路徑并將其存儲(chǔ)在字符串?dāng)?shù)組中。
- 創(chuàng)建PdfDocument數(shù)組,并將每個(gè)源文檔加載到單獨(dú)的PdfDocument對(duì)象中。
- 創(chuàng)建另一個(gè)PdfDocument對(duì)象來(lái)生成新文檔。
- 使用PdfDocument.InsertPage()方法和PdfDocument.InsertPageRange()方法將源文檔的選定頁(yè)面或頁(yè)面范圍插入到新文檔中。
- 使用PdfDocument.SaveToFile()方法將新文檔保存為 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
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-845151.html
以上便是如何合并 PDF 文件并添加頁(yè)碼,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-845151.html
到了這里,關(guān)于Spire.PDF for .NET【文檔操作】演示:合并 PDF 文件并添加頁(yè)碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!