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

相关推荐
Themberfue3 分钟前
基础算法之双指针--Java实现(下)--LeetCode题解:有效三角形的个数-查找总价格为目标值的两个商品-三数之和-四数之和
java·开发语言·学习·算法·leetcode·双指针
深山夕照深秋雨mo12 分钟前
在Java中操作Redis
java·开发语言·redis
努力的布布17 分钟前
SpringMVC源码-AbstractHandlerMethodMapping处理器映射器将@Controller修饰类方法存储到处理器映射器
java·后端·spring
xujinwei_gingko18 分钟前
Spring MVC 常用注解
java·spring·mvc
PacosonSWJTU22 分钟前
spring揭秘25-springmvc03-其他组件(文件上传+拦截器+处理器适配器+异常统一处理)
java·后端·springmvc
PacosonSWJTU24 分钟前
spring揭秘26-springmvc06-springmvc注解驱动的web应用
java·spring·springmvc
津津有味道1 小时前
VB.net读写NDEF标签URI智能海报WIFI蓝牙连接
.net·nfc·uri·ndef·智能海报·电子标签
原野心存1 小时前
java基础进阶——继承、多态、异常捕获(2)
java·java基础知识·java代码审计
进阶的架构师1 小时前
互联网Java工程师面试题及答案整理(2024年最新版)
java·开发语言
黄俊懿1 小时前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构