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
将不同 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

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

相关推荐
西西学代码19 分钟前
Flutter---Stream
java·服务器·flutter
Blossom.1183 小时前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
静若繁花_jingjing3 小时前
IDEA下载
java·ide·intellij-idea
代码丰3 小时前
函数式接口+default接口+springAi 中的ducumentReader去理解为什么存在default接口的形式
java
果汁华5 小时前
java学习连续打卡30天(1)
java
武子康5 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
m0_639817155 小时前
基于springboot火锅店管理系统【带源码和文档】
java·spring boot·后端
会编程的林俊杰6 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet7 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
王家羽翼-王羽7 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java