合并 PDF 文档
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。
欢迎加入spire技术交流群:767755948
合并 PDF 有许多必要的原因。例如,合并 PDF 文件可让您打印单个文件,而无需为打印机排队打印多个文件;通过减少需要搜索和整理的文件数量,合并相关文件可简化管理和存储多个文件的过程。在本文中,您将学习如何在 C# 和 VB.NET 中使用 Spire.PDF for .NET 将多个 PDF 文档合并为一个 PDF 文档,以及如何将不同 PDF 文档中的选定页面合并为一个 PDF。- 将多个 PDF 合并为一个 PDF
- 将不同 PDF 中的选定页面合并为一个 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文件。
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
申请临时许可证
若想从生成的文档中删除评估信息,或解除功能限制,申请 30 天试用许可证

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线