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

本文介绍了如何利用Spire.PDFfor.NET库在.NET程序中将多个PDF文档合并为一个,以及如何选取和合并不同PDF文档的特定页面。提供了C#和VB.NET示例代码,展示了如何添加DLL引用和执行合并操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要合并 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文件,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

Free Spire.PDF for .NETSpire.PDF for .NET 的免费版本,无需购买即可用于个人或商业用途。使用该组件,程序员可以 在.NET 程序中创建、读取、写入、编辑和操作 PDF 文档。这个控件能支持的功能十分全面,例如文档安全性设置(电子签名),提取 PDF 文本、附件、图片,PDF 合并和拆分,更新 Metadata,设置 Section,绘制图形、插入图片、表格制作和加工、导入数据等等。除此以外,Spire.PDF 还可以将 TXT 文本、图片、HTML 高质量地转换为 PDF 文件格式。 主要功能如下: 1.高质量的文档转换。Free Spire.PDF for .NET 支持 PDF 到 Word、XPS、SVG、EMF、Text 和图片(EMF、JPG、PNG、BMP、TIFF)的格式转换。也支持从 XML、HTML、RTF、XPS、Text、图片等格式生成 PDF 文档。 2.文档操作及域功能。支持合并、拆分 PDF 文档,在原有的 PDF 文档页添加覆盖页。同时,Spire.PDF 提供导入、邮戳、小册子功能,以及帮助用户从数据库读取数据并填充到域的域填写功能。 3. 安全性设置。用户可以通过设置密码和数字签名来保护 PDF 文档。用户密码和所有者密码可以确定加密的 PDF 文档的可读性、可修改性、是否可打印等有选择性的限制。与此同时,数字签名作为一个更有效的方法,可以应用于维护和对PDF文档进行身份验证。 4.数据提取。支持快速高效地从 PDF 文档提取图片、文本、PDF 分页,以及附件。 5.文件属性设置。支持对 Metadata、文件属性、页面方向、页面大小进行设置。其中文件属性包括文件限制(打印、页面提取、加评论等方面的权限限制)以及文件描述属性(文件名称、作者、主题、关键字等)。使用 Spire.PDF for .NET,用户还可以根据自己阅读喜好设定默认打开页码,分页模式,缩放比例和打印缩放,等等。 6.其他功能。 支持多种语言,支持字体格式、对齐方式设置。 绘制文字,图片,图形。 支持添加图层,透明图像,Color Space,条形码到 PDF。 支持 PDF/A-1b、PDF/x1a:2001 格式。 添加梯状图形和矢量图像到指定位置。 添加并格式化表格。 插入交互元素,例如添加自定义的 Annotation、Action、JavaScript、附件、书签等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值