Spire.PDF for .NET【文档操作】演示:将PDF拆分为多个PDF文件

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式**(qun:767755948)**

Spire.PDF for.net下载 Spire.PDF for java下载

安装适用于 .NET 的 Spire.PDF

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

复制代码
PM> Install-Package Spire.PDF
使用 C#、VB.NET 将 PDF 拆分为一页 PDF

Spire.PDF提供Split()方法将多页PDF文档分割成多个单页文件。以下是详细步骤。

  • 创建一个PdfDcoument对象。
  • 使用PdfDocument.LoadFromFile()方法加载 PDF 文档。
  • 使用PdfDocument.Split(string destFilePattern, int startNumber)方法将文档拆分为一页 PDF 。

[C#]

复制代码
using System;
using Spire.Pdf;

namespace SplitPDFIntoIndividualPages
{
class Program
{
static void Main(string[] args)
{
//Specify the input file path
String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf";

//Specify the output directory
String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\";

//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Load a PDF file
doc.LoadFromFile(inputFile);

//Split the PDF to one-page PDFs
doc.Split(outputDirectory + "output-{0}.pdf", 1);
}
}
}

VB.NET

复制代码
Imports System
Imports Spire.Pdf

Namespace SplitPDFIntoIndividualPages
Class Program
Shared Sub Main(ByVal args() As String)
'Specify the path of the input file
Dim inputFile As String = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"

'Specify the output directory
Dim outputDirectory As String = "C:\\Users\\Administrator\\Desktop\\Output\\"

'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Load a PDF file
doc.LoadFromFile(inputFile)

'Split the PDF to one-page PDFs
doc.Split(outputDirectory + "output-{0}.pdf", 1)
End Sub
End Class
End Namespace
在 C#、VB.NET 中按页面范围拆分 PDF

没有提供按页面范围拆分 PDF 文档的直接方法。为此,我们创建两个或多个新的 PDF 文档,并将源文档中的页面或页面范围导入到其中。以下是详细步骤。

  • 初始化PdfDocument对象时加载源 PDF 文件。
  • 创建两个额外的PdfDocument对象。
  • **使用PdfDocument.InsertPage()**方法将第一页从源文件导入到第一个文档。
  • **使用PdfDocument.InsertPageRange()**方法将剩余页面从源文件导入到第二个文档。
  • **使用PdfDocument.SaveToFile()**方法将两个文档保存为单独的 PDF 文件。

[C#]

复制代码
using Spire.Pdf;
using System;

namespace SplitPdfByPageRanges
{
class Program
{
static void Main(string[] args)
{
//Specify the input file path
String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf";

//Specify the output directory
String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\";

//Load the source PDF file while initialing the PdfDocument object
PdfDocument sourceDoc = new PdfDocument(inputFile);

//Create two additional PdfDocument objects
PdfDocument newDoc_1 = new PdfDocument();
PdfDocument newDoc_2 = new PdfDocument();

//Insert the first page of source file to the first document
newDoc_1.InsertPage(sourceDoc, 0);

//Insert the rest pages of source file to the second document
newDoc_2.InsertPageRange(sourceDoc, 1, sourceDoc.Pages.Count - 1);

//Save the two documents as PDF files
newDoc_1.SaveToFile(outputDirectory + "output-1.pdf");
newDoc_2.SaveToFile(outputDirectory + "output-2.pdf");
}
}
}

VB.NET

复制代码
Imports Spire.Pdf
Imports System

Namespace SplitPdfByPageRanges
Class Program
Shared Sub Main(ByVal args() As String)
'Specify the input file path
Dim inputFile As String = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"

'Specify the output directory
Dim outputDirectory As String = "C:\\Users\\Administrator\\Desktop\\Output\\"

'Load the source PDF file while initialing the PdfDocument object
Dim sourceDoc As PdfDocument = New PdfDocument(inputFile)

'Create two additional PdfDocument objects
Dim NewDoc_1 As PdfDocument = New PdfDocument()
Dim NewDoc_2 As PdfDocument = New PdfDocument()

'Insert the first page of source file to the first document
NewDoc_1.InsertPage(sourceDoc, 0)

'Insert the rest pages of source file to the second document
NewDoc_2.InsertPageRange(sourceDoc, 1, sourceDoc.Pages.Count - 1)

'Save the two documents as PDF files
NewDoc_1.SaveToFile(outputDirectory + "output-1.pdf")
NewDoc_2.SaveToFile(outputDirectory + "output-2.pdf")
End Sub
End Class
End Namespace

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

相关推荐
Vcats2 分钟前
深入浅出:基于SpringBoot和JWT的后端鉴权系统设计与实现
java·spring boot·后端
菠菠萝宝6 分钟前
【Java八股文】11-分布式及场景面试篇
java·分布式·面试·k8s·系统·uuid·mq
LUCIAZZZ14 分钟前
Https解决了Http的哪些问题
java·网络·网络协议·spring·http·rpc·https
zhang_adrian34 分钟前
.Net 9下使用Tensorflow.net---DNN_Eager
tensorflow·.net·dnn
论迹35 分钟前
【JavaEE】-- 多线程(初阶)2
java·开发语言·java-ee
桃子是唯一的水果44 分钟前
java 单例模式(Lazy Initialization)实现遍历文件夹下所有excel文件且返回其运行时间
java·单例模式·maven
+7201 小时前
如何在java中用httpclient实现rpc post 请求
java·开发语言·rpc
ybq195133454311 小时前
javaEE-SpringBoot日志
java·spring boot·后端
火烧屁屁啦1 小时前
【JavaEE进阶】图书管理系统 - 贰
java·spring
xzzd_jokelin1 小时前
Spring AI 接入 DeepSeek:开启智能应用的新篇章
java·人工智能·spring·ai·大模型·rag·deepseek