将 PDF 分割成多个 PDF 文件
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。
欢迎加入spire技术交流群:767755948
在某些情况下,将单个 PDF 文件分割成多个较小的 PDF 文件会很有帮助。例如,您可以将大型合同、报告、书籍、学术论文或其他文档分割成小块,以便于审查或重用。在本文中,您将学习如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中将 PDF 分割成单页 PDF 以及如何按页面范围分割 PDF。
- 将 PDF 拆分为单页 PDF
- 按页面范围分割 PDF
安装 Spire.PDF for .NET
首先,您需要将 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 文件。
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 文档,并将源文件中的页面或页面范围导入其中。以下是详细步骤。
- 加载源PDF文件,同时初始化PdfDocument对象。
- 创建两个额外的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
申请临时许可证
若想从生成的文档中删除评估信息,或解除功能限制,申请 30 天试用许可证。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线