.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 ...
随机推荐
- 关于oop的一点回忆
昨天在一个程序员行业群里看到别人发了一条消息, 大意是:要做好封装啦,不要随便用public啦,不要随便改别人代码啦. 说的好像就是我,因为,我这辈子最后悔的一件事情之一就是手贱改动别人代码. 那大概 ...
- paramiko免密登陆
paramiko免密登陆 # -*- coding: utf-8 -*- import paramiko pkey='D:/pycharm_workspace/testpy/ssh_paramiko_ ...
- 算法打卡|Day5 哈希表part01
哈希表 part01 今日任务 ● 哈希表理论基础 ● 242.有效的字母异位词 ● 349. 两个数组的交集 ● 202. 快乐数 ● 1. 两数之和 目录 哈希表 part01 链表理论基础 Pr ...
- java数组的定义和使用规范
java数组 三种定义方式 1.数组类型[] 数组名字 = new 数组类型[数组长度] String[] str = new String[n]; //这里n代表数组的长度可变 //另外上面这种写法 ...
- Decorator 装饰者模式简介与 C# 示例【结构型4】【设计模式来了_9】
〇.简介 1.什么是装饰者模式 一句话解释: 通过继承统一的抽象类来新增操作,再在使用时通过链式添加到对象中,达到与原有设定无关联可灵活附加. 装饰者模式是一种行为设计模式,它允许向一个现有的对象 ...
- Little Victor and Set 题解
Little Victor and Set 题目大意 在 \([l,r]\) 中选不超过 \(k\) 个相异的数使得异或和最小,输出方案. 思路分析 分类讨论: 当 \(k=1\) 时: 显然选 \( ...
- [ABC310D] Peaceful Teams 题解
Peaceful Teams 题目大意 将 \(n\) 个人分成 \(T\) 组,要求每组不能包含敌对的人,问有多少种分法. 思路分析 注意到 \(n,T\) 均很小,考虑爆搜. 注意到直接枚举会枚举 ...
- [ABC208E] Digit Products 题解
Digit Products 题目大意 求有多少个不大于 \(n\) 的正整数,使得该正整数各位乘积不大于 \(k\). 思路分析 观察数据范围,首先考虑数位 DP. 考虑设计记忆化搜索函数 dfs( ...
- VS Code C# 开发工具包正式发布
前言 微软于本月正式发布Visual Studio Code C#开发工具包,此前该开发套件已经以预览版的形式在6月份问世.经过4个月的测试和调整,微软修复了350多个问题,其中大部分是用户反馈导致的 ...
- 什么是yaml格式与json格式
什么是yaml格式与json格式 yaml格式:文件名格式以 .yml .yaml 为后缀,用 空格 缩进表示字段的层级关系,可读性高,易于人类管理 yaml格式 布尔值类型:只有在是true/fal ...