翻譯|行業(yè)資訊|編輯:胡濤|2024-04-15 13:31:47.297|閱讀 102 次
概述:在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET將多個(gè) PDF 文檔合并為一個(gè) PDF 文檔,以及如何使用C# 和 VB.NET將不同 PDF 文檔中的選定頁(yè)面合并為一個(gè) PDF 。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
需要合并 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ā),不依賴第三方軟件,不受其他國(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下載
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目中的引用。 DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.PDF
Spire.PDF for .NET 提供PdfDocument.MergeFiles()方法將多個(gè) PDF 文檔合并為單個(gè)文檔。詳細(xì)步驟如下。
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()方法,用于將頁(yè)面或頁(yè)面范圍從一個(gè) PDF 文檔導(dǎo)入到另一個(gè) PDF 文檔。以下是將不同 PDF 文檔中的選定頁(yè)面合并為一個(gè)新 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文件,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn