.NET 8上进行PDF合并
前言:在.NET 8中使用itext7需安装 itext7 和 itext7.bouncy-castle-fips-adapter 两个Nuget包,或者使用Aspose.PDF、PdfSharpCore、Spire.PDF任意一个第三方包就行
对比:
| 第三方包 | 是否开源 | 是否收费 |
| itext7 | 是 | 不收费,免费使用没有水印 |
| PdfSharpCore | 是 | 不收费,免费使用没有水印 |
| Spire.PDF | 否 | 是,可以免费使用但有水印 |
| Aspose.PDF | 否 | 是,可以免费使用但有水印 |
安装如下Nuget的包
<ItemGroup>
<PackageReference Include="Aspose.PDF" Version="23.11.1" />
<PackageReference Include="itext7" Version="8.0.2" />
<PackageReference Include="itext7.bouncy-castle-fips-adapter" Version="8.0.2" />
<PackageReference Include="PdfSharpCore" Version="1.3.62" />
<PackageReference Include="Spire.PDF" Version="9.12.0" />
</ItemGroup>
项目结构示意图

一、itext7

using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PdfTest.PdfHelpers
{
public class iText7Helper
{
// https://blog.csdn.net/m0_47619208/article/details/130725907
/// <summary>
/// 合并多个PDF文档
/// 添加NuGet包引用:添加itext7和itext7.bouncy-castle-adapter,后者必须进行添加,否则会在运行报错。
/// NuGet包为iText7,该库满足跨平台需求
/// https://itextpdf.com/
/// https://itextpdf.com/itext-suite-net-c
/// </summary>
/// <param name="fileList"></param>
/// <param name="outFile"></param>
/// <returns></returns>
public static Task MergePdf(string[] fileList, string outFile)
{
using PdfWriter writer = new(outFile);
using PdfDocument pdf = new(writer);
PdfMerger merger = new(pdf);
for (int i = 0; i < fileList.Length; i++)
{
PdfReader reader = new(fileList[i]);
using PdfDocument pdfDocument = new(reader);
merger.Merge(pdfDocument, 1, pdfDocument.GetNumberOfPages());
}
merger.Close();
return Task.CompletedTask;
}
}
}
二、PdfSharpCore

using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PdfTest.PdfHelpers
{
public class PdfSharpTool
{
// https://github.com/ststeiger/PdfSharpCore public static void PdfSharpCoreMerge(string[] files, string outFile)
{
using PdfDocument outPdfFile = new PdfDocument();
foreach (var file in files)
{
using PdfDocument pdfFile = PdfReader.Open(file, PdfDocumentOpenMode.Import); foreach (var pdfPage in pdfFile.Pages)
{
outPdfFile.AddPage(pdfPage);
}
}
outPdfFile.Save(outFile);
} }
}
三、Aspose.PDF

using Aspose.Pdf.Facades;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PdfTest.PdfHelpers
{
internal class AsposePdfHelper
{
//https://blog.aspose.com/zh/pdf/merge-multiple-pdf-files-in-csharp-net/
//收费
public static void Merge(string[] filesArray, string outFile)
{
// 创建 PdfFileEditor 对象
PdfFileEditor pdfEditor = new PdfFileEditor();
// 合并文件
//pdfEditor.Concatenate("file1.pdf", "file2.pdf", "merged.pdf");
// 合并文件
pdfEditor.Concatenate(filesArray, outFile); } public static void MergeStream(FileStream[] streamArray, string outFile)
{
// 创建 PdfFileEditor 对象
PdfFileEditor pdfEditor = new PdfFileEditor();
// 输出流
//FileStream outputStream = new FileStream("Merged.pdf", FileMode.Create);
FileStream outputStream = new FileStream(outFile, FileMode.Create);
//// 输入流
//FileStream inputStream1 = new FileStream("file1.pdf", FileMode.Open);
//FileStream inputStream2 = new FileStream("file2.pdf", FileMode.Open);
//// 合并文件
//pdfEditor.Concatenate(inputStream1, inputStream2, outputStream); // 创建流数组
//FileStream[] streamArray = new FileStream[3];
//streamArray[0] = new FileStream("file1.pdf", FileMode.Open);
//streamArray[1] = new FileStream("file2.pdf", FileMode.Open);
//streamArray[2] = new FileStream("file3.pdf", FileMode.Open);
// 合并文件
pdfEditor.Concatenate(streamArray, outputStream); } }
}
四、Spire.PDF

using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace PdfTest.PdfHelpers
{
public class SpirePdfHelper
{
//https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html
//https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
//https://www.cnblogs.com/dongweian/p/14305928.html
//有水印 /// <summary>
/// 合并PDF文件
/// </summary>
/// <param name="files">待合并文件列表</param>
/// <param name="outFile">合并生成的文件名称</param>
public static void SpirePdfMerge(string[] files, string outFile)
{
var doc = PdfDocument.MergeFiles(files);
doc.Save(outFile, FileFormat.PDF);
} /// <summary>
/// 按每页拆分PDF文件
/// </summary>
/// <param name="inFile">待拆分PDF文件名称</param>
public static void SpirePdfSplit(string inFile)
{
var doc = new PdfDocument(inFile);
doc.Split("SpirePdf_拆分-{0}.pdf");
doc.Close();
} }
}
在控制台上调用
using PdfTest.PdfHelpers; namespace PdfTest
{
internal class Program
{
static void Main(string[] args)
{
// 文件数组索引在前面的后,合并后的PDF,页面也在前面
string [] files = Directory.GetFiles("Merge");
#region Spire.Pdf
//SpirePdfHelper.SpirePdfMerge(files, "SpirePdfMerge.pdf");
//Console.WriteLine("使用 Spire.Pdf 合并文件完成...");
#endregion #region PdfSharpCore
//PdfSharpTool.PdfSharpCoreMerge(files, "PdfSharpCoreMerge.pdf");
//Console.WriteLine("使用 PdfSharpCore 合并文件完成...");
#endregion #region iText7
//iText7Helper.MergePdf(files, "iText7PdfMerge.pdf");
//Console.WriteLine("使用 iText7 合并文件完成...");
#endregion #region AsposePdf
AsposePdfHelper.Merge(files, "AsposePdfMerge.pdf");
Console.WriteLine("使用 AsposePdf 合并文件完成...");
#endregion Console.ReadKey();
}
}
}
.NET 8上进行PDF合并的更多相关文章
- SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework
1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...
- myeclipse上SVN代码合并详细步骤图解
1. 在装有svn插件的myeclipse中,在主干上选择需要合并的文件或文件夹 右击 -> 合并(merge) 2. 选择合并类型--合并两个不同的树 Merge -> Next 3. ...
- PDF合并
要求:将多个table导出到一个PDF里,然后打印. 问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个exce ...
- mvc在页面上显示PDF
今天看到需求要在页面上显示pdf,自己整了半天,啥效果都没有,偶尔有效果还各种不兼容,很无语的说.捣鼓了半天,没办法了,去谷歌了下,介绍了各种插件,各种方法,但是都挺繁琐的,本人不是一个很喜欢使用插件 ...
- 【O】VSS 2005上传PDF文件之后,打开提示文件损坏或者内容为空
问题: VSS 2005上传PDF文件之后,打开提示文件损坏或者内容为空: 解决方式: 在vss的客户端的tools-option中,file type选项卡里,在binary file文本框中,加入 ...
- C#实现多个PDF合并及去除文字水印功能
实现pdf合并就是使用Spire.Pdf.dll类库的方法,但是注意需要同时引用Spire.Pdf.dll和Spire.License.dll两个类库,且两个类库的版本要一致 String[] fil ...
- git----------如何创建develop分支和工作流,以及如何将develop上的代码合并到master分支上
1.点击sourceTree 右上角的git工作流,或弹出一个弹出框,无需修改任何东西直接点击确认就可以创建develop. . 2.这里有两个分支了,当前高亮的就是你当前处在的分支.此时develo ...
- 在 chrome 上导出 pdf
用html+css写出网页,然后在chrome上导出pdf 1. command + p:快捷呼出打印: 2. “目标打印机”:选择“更改”,之后选择“另存为PDF”: 3. 点“更多设置”,可以勾选 ...
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...
- pdftk - handy tool for manipulating PDF 免费的pdf合并工具
Linux pdf合并的工具 安装工具 $ sudo apt-get install pdftk 使用 $ pdftk *.pdf cat output all-in-one.pdf &&am ...
随机推荐
- 深入理解HTTP的基础知识:请求-响应过程解析
首先,我们从网络协议的最顶层开始讲解,即应用层.在网络通信中,应用层是最接近用户的一层,它负责为特定的网络应用提供服务和功能.应用层协议定义了数据交换的规则和格式,以便不同的应用程序能够相互通信和交换 ...
- Python Flask 上下文管理源码分析
Python Flask 上下文管理源码分析 前言 Flask 上下文管理可以说是 Flask 非常具有特色的设计,它总共可分为 2 个大的方向: 应用上下文管理:通过 current_app 即可拿 ...
- dedebiz实时时间调用
{dede:tagname runphp='yes'}@me = date("Y-m-d H:i:s", time());{/dede:tagname}
- c语言代码练习5
//输入密码,正确就登录,错误重新输入,只能输入三次#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <st ...
- [NISACTF 2022]level-up
[NISACTF 2022]level-up 查看源码,根据这个提示就可以反应出是需要去访问robots.txt这个文件 访问level_2_1s_h3re.php进入第二关 需要post进去arra ...
- css 10-13
1.背影样式 backgroud-color 背景颜色 backgroud-color :red backgroud-image 背 ...
- Java并发编程和多线程的区别
并发编程: 并发编程是一种编程范式,它关注的是编写能够正确和高效处理多个并发任务的程序.并发编程不仅包括多线程,还包括了处理多个独立任务的各种技术和模式,如进程.协程.分布式编程等.并发编程的目标是实 ...
- 每天5分钟复习OpenStack(六)CPU虚拟化<2>
OpenStack是 一个IAAS(基础设施即服务)因此免不了会与硬件打交道.下面我介绍下与CPU强关联的一些知识点.1 什么是超配 2 CPU的个数是怎么统计的 3 vCPU的隔离.绑定 1.超配 ...
- 虹科案例 | 丝芙兰xDomo:全球美妆巨头商业智能新玩法
全球美妆行业的佼佼者丝芙兰,其走向成功绝非仅依靠品牌知名度和营销手段.身为数据驱动型企业,2018年以来,丝芙兰就率先在行业内采用虹科提供的Domo商业智能进行数据分析和决策,并首先享受了运营优化.效 ...
- Python 面向对象编程:类、对象、初始化和方法详解
Python 是一种面向对象的编程语言.在 Python 中,几乎所有东西都是对象,都具有其属性和方法. 类似于对象构造函数或用于创建对象的"蓝图"的类. 创建一个类 要创建一个类 ...