当前位置:网站首页>C#/VB.NET 合并PDF文档

C#/VB.NET 合并PDF文档

2022-07-01 15:32:00 InfoQ

在日常工作中我们经常会遇到将多个PDF文档合并在一起的情况。大多数人都会选择先将PDF转换为Word文档,再直接复制粘贴,最后再转为PDF文档。这种方法不仅比较麻烦,而且有时候可能会造成排版错乱的情况。今天给大家介绍的方法是通过C#/VB.NET代码来实现此功能,可以既快速又简单地合并PDF文档,最重要的是不会造成格式错误和排版错乱的情况,达到较好的合并效果。本文将为您介绍两种合并PDF文档的方法。下面是我整理的思路及详细步骤,并附上C#/VB.NET代码供大家参考,希望能对大家有所帮助。
将多个PDF合并为一个PDF文档
将不同 PDF的指定页面合并为一个 PDF文档
程序环境:
本次测试时,在程序中引入 Spire.PDF.dll 文件。
 
方法 1:
将FreeSpire.PDF for .NET 下载到本地,解压,找到 BIN 文件夹下的 Spire.PDF.dll。然后在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径 BIN 文件夹下的 dll 文件添加引用至程序。
 
方法 2:
通过 
NuGet
 安装。可通过以下 2 种方法安装:
 
  1. 可以在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet 包”,然后搜索“Free Spire.PDF”,点击“安装”。等待程序安装完成。
 
  2. 将以下内容复制到 PM 控制台安装。
 
Install-Package FreeSpire.PDF-Version 8.6.0

将多个PDF合并为一个PDF文档

具体步骤:
  • 获取要合并的文档的路径并将它们存储在字符串数组中。
  • 调用PdfDocument.MergeFiles()方法合并文档。
  • 用PdfDocumentBase.Save()方法另存为一个PDF文档
完整代码:
C#】
using System;
using Spire.Pdf;

namespace MergePDFs
 {
 class Program
 {
 static void Main(string[] args)
 {
 //获取要合并的文档的路径
 String[] files = new String[] {
 "Sample.pdf",
 "Sample1.pdf",
 "Samplex.pdf"
 //合并文档
 PdfDocumentBase doc = PdfDocument.MergeFiles(files);

 //将结果保存到 PDF文件
 doc.Save("MergeDocuments.pdf", FileFormat.PDF);
 }
 }
}
VB.NET
Imports System
Imports Spire.Pdf

Namespace MergePDFs
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
 '获取要合并的文档的路径
 Dim files() As String = New String() {" Sample.pdf", "Sample-1.pdf", "Samplex.pdf"}
 '合并文档
 Dim doc As PdfDocumentBase = PdfDocument.MergeFiles(files)
 '将结果保存到 PDF文件
 doc.Save("output.pdf", FileFormat.PDF)
 End Sub
 End Class
End Namespace
 效果图:

将不同 PDF的指定页面合并为一个 PDF文档

具体步骤:
  • 获取源文档的路径并将它们存储在字符串数组中。
  • 创建一个 PdfDocument 数组,并将每个源文档加载到单独的 PdfDocument 对象。
  • 使用PdfDocument.InsertPage() 方法和 PdfDocument.InsertPageRange() 方法将源文档的指定页面插入到新文档中。
  • 用PdfDocument.SaveToFile()方法将新文档保存为PDF。
完整代码:
【C#】
using System;
using Spire.Pdf;

namespace MergeSelectedPages
{
 class Program
 {
 static void Main(string[] args)
 {
 //获取要合并的文档的路径
 String[] files = new String[] {
 "Sample1.pdf",
 "Sample.pdf",
 "Sample2.pdf"};

 //创建一个 PdfDocument 数组
 PdfDocument[] docs = new PdfDocument[files.Length];

 //循环浏览文档
 for (int i = 0; i < files.Length; i++)
 {
 //加载指定文档
 docs[i] = new PdfDocument(files[i]);
 }

 //创建一个 PdfDocument 对象以生成新的 PDF 文档
 PdfDocument doc = new PdfDocument();

 //将不同文档中的指定页面插入到新文档中
 doc.InsertPage(docs[0], 0);
 doc.InsertPageRange(docs[1], 0, 2);
 doc.InsertPage(docs[2], 1);

 //保存文档为PDF文件
 doc.SaveToFile(&quot;output.pdf&quot;);
 }
 }
}
【VB.NET】
Imports System
Imports Spire.Pdf

Namespace MergeSelectedPages
 
 Class Program
 
 Private Shared Sub Main(ByVal args() As String)
 '获取要合并的文档的路径
 Dim files() As String = New String() {&quot;Sample1.pdf&quot;, &quot;Sample.pdf&quot;, &quot;Sample2.pdf&quot;}
 '创建一个 PdfDocument 数组
 Dim docs() As PdfDocument = New PdfDocument((files.Length) - 1) {}
 '循环浏览文档
 Dim i As Integer = 0
 Do While (i < files.Length)
 '加载指定文档
 docs(i) = New PdfDocument(files(i))
 i = (i + 1)
 Loop
 
 '创建一个 PdfDocument 对象以生成新的 PDF 文档
 Dim doc As PdfDocument = New PdfDocument
 '将不同文档中的指定页面插入到新文档中
 doc.InsertPage(docs(0), 0)
 doc.InsertPageRange(docs(1), 0, 2)
 doc.InsertPage(docs(2), 1)
 '保存文档为PDF文件
 doc.SaveToFile(&quot;output.pdf&quot;)
 End Sub
 End Class
End Namespace
效果图:

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/2ce00107f7fc7f17bd8c5b41a