Spire.PDF for .NET【文档操作】演示:合并 PDF 文档

需要合并 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下载

安装适用于 .NET 的 Spire.PDF

首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。 DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.PDF
将多个 PDF 合并为一个 PDF

Spire.PDF for .NET 提供PdfDocument.MergeFiles()方法将多个 PDF 文档合并为单个文档。详细步骤如下。

  • 获取要合并的文档的路径并将其存储在字符串数组中。
  • 调用PdfDocument.MergeFiles()方法来合并这些文件。
  • 使用PdfDocumentBase.Save()方法将结果保存到 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

C#/VB.NET:合并 PDF 文档

将不同 PDF 的选定页面合并为一个 PDF

Spire.PDF for .NET 提供PdfDocument.InsertPage()方法和PdfDocument.InsertPageRange()方法,用于将页面或页面范围从一个 PDF 文档导入到另一个 PDF 文档。以下是将不同 PDF 文档中的选定页面合并为一个新 PDF 文档的步骤。

  • 获取源文档的路径并将其存储在字符串数组中。
  • 创建PdfDocument数组,并将每个源文档加载到单独的PdfDocument对象中。
  • 创建另一个PdfDocument对象来生成新文档。
  • 使用PdfDocument.InsertPage()方法和PdfDocument.InsertPageRange()方法将源文档的选定页面或页面范围插入到新文档中。
  • 使用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

C#/VB.NET:合并 PDF 文档

以上便是如何合并 PDF文件,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Free Spire.PDF for .NET 是一个用于.NET平台的免费PDF处理库。它提供了一套强大的功能,可以用于创建、编辑和处理PDF文档。无论是在Windows应用程序还是Web应用程序中,Free Spire.PDF for .NET 都能帮助我们轻松实现对PDF文件的操作。 首先,Free Spire.PDF for .NET 支持创建和编辑PDF文档。我们可以使用库中提供的API来创建新的PDF文档,添加页面、文本、图像和表格等内容,并设置页面的布局和样式。同时,我们还可以使用库中的功能来编辑现有的PDF文档,例如添加、提取和删除页面,修改文本、图像和表格等。 其次,Free Spire.PDF for .NET 还提供了丰富的PDF处理功能,可以帮助我们完成一些特定的操作。例如,我们可以使用库中的API来提取、合并和拆分PDF文档,将多个PDF文件合并为一个,或者将一个PDF文件拆分为多个部分。此外,库中还提供了加密和解密PDF文档的功能,可以帮助我们保护敏感的PDF文件内容。 除了基本的PDF处理功能之外,Free Spire.PDF for .NET 还支持高级功能,如转换PDF文档到其他格式,例如HTML、Word、Excel和图像等。我们可以使用库中的API将PDF文件转换为其他格式的文件,便于我们在不同的应用程序中使用。 总之,Free Spire.PDF for .NET 是一个功能强大、易于使用且免费的PDF处理库。它提供了一系列实用的API,可以帮助我们创建、编辑和处理PDF文档,完成各种PDF操作。无论是在个人项目中还是商业应用中,Free Spire.PDF for .NET 都是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值