当前位置:网站首页>C#实现给PDF文档设置过期时间
C#实现给PDF文档设置过期时间
2022-07-21 19:04:00 【王陈锋】
目录
前情提要
接触的事情奇奇怪怪,我学得东西也是奇奇怪怪。
我需要搭建环境测试代码是否能成功操作大型的pdf文件。
环境搭配
踩坑
visual studio code就别想了,这玩意没法下载相关的pdf的依赖库
IntelliJ IDEA也别想,这个不支持C#。
visual studio真是大爷,安装几台的虚拟机,都说不支持当前的windows 10系统。
要说为什么了话,因为这些坑,我自己一个个去趟过去。。。
采用环境
win10的2016企业版
visual studio2022
在visual studio2022中,工具->管理NuGet包->搜索“Free Spire.PDF”->点击安装
安装版本为7.8.9
代码实现
代码思路
PDF文档添加过期时间
- 创建PdfDocument类的对象,并通过PdfDocument.LoadFromFile(string filename)方法加载PDF文档。
- 通过JavaScript语句设置过期时间,过期警告信息并关闭文档等动作。
- 初始化PdfJavaScriptAction类的实例,并通过PdfDocument.AfterOpenAction属性设置PDF打开后执行JS语句命令。
- 最后,通过PdfDocument.SaveToFile(string filename, FileFormat fileFormat)方法保存PDF文档到指定路径。
using Spire.Pdf;
using Spire.Pdf.Actions;
using System.Drawing;
namespace SetExpiryDate
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument类的对象,并加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("test.pdf");
//通过JavaScript设置过期时间,过期警告信息并关闭文档
string javaScript = "var rightNow = new Date();"
+ "var endDate = new Date('June 20, 2021 23:59:59');"
+ "if(rightNow.getTime() > endDate) {"
+ "app.alert('该文档已过期。请联系管理人员获取最新文档!',1);"
+ "this.closeDoc(); }";
//根据JS创建PdfJavaScriptAction
PdfJavaScriptAction js = new PdfJavaScriptAction(javaScript);
//将PdfJavaScriptAction设置为文档打开后的动作
pdf.AfterOpenAction = js;
//保存文档
pdf.SaveToFile("SetExpiryDate.pdf", FileFormat.PDF);
}
}
}
相关操作
新建项目,代码导入
制作个word,然后转pdf
将test.pdf导入到项目对应位置
然后点击运行
代码结果
然后回到之前放test.pdf文件的文件夹中,有个叫SetExpiryDate的文件,就是我们操作之后的文件。
双击打开
后期探查
在后期反复实验的时候,我发现pdf文档不能过大,我就直接用200与300多MB的PDF去测试,均以失败告终。
然后去调试,发现是FreeSpire.PDF的问题,它限制了PDF页数不能超过10页。
不过一般论文是可以成功运行的,我有去测试过。
边栏推荐
- Intel joins hands with Baidu to promote the process of industrial intelligence and build a green future together
- Ingress
- AtCoder Beginner Contest 260 E - At Least One
- List iterator
- HCIP(10)
- Std:: thread combined with class object
- [Basic] Pointer in C
- 【信息系統項目管理師】第四章 複盤整體管理知識架構
- [information system project manager] Chapter IV comprehensive management knowledge structure
- Recommend the source code of the finished live broadcast, and set the system date, time and time zone
猜你喜欢
Execution failed for task ‘:app:kaptDevDebugKotlin‘.
Some visualization methods of CNN
[cicadaplayer] vs2019 (v142) x64 ffmepg: error c2169: "lrintf": internal function, cannot be defined
仅需3 小时,如何用 AI 做场景贴图,完成场景制作 ?AI创作工作流探索
Embedded bus and communication
这项比赛有点逆天!鲲鹏应用创新大赛openEuler赛道全面开启
输入密码才可以浏览网页的PHP源码
[814. Binary tree pruning]
Service & Endpoint
Recommendation of top 10 mainstream pressure / load / performance testing tools
随机推荐
[explanation of PTA program design topic] [English version] [6-5 use function to verify Goldbach conjecture]
Unity Scrollview scroll to the specified position
梨花带雨音乐播放器3.91源码开源(网站添加背景音乐)
day02-2
SQLite in the flutter tutorial_ Wrapper new dart and shutter libraries for SQLite
DaVinci resolve 18, a video editing and color matching software, was officially released
Intel joins hands with Baidu to promote the process of industrial intelligence and build a green future together
HCIP(10)
标准输入输出流
【信息系统项目管理师】第四章 复盘整体管理知识架构
QUuid
D extend CTFE at compile time
[information system project manager] Chapter IV comprehensive management knowledge structure
基于卡尔曼滤波的微电网调度(Matlab代码实现)
【信息系統項目管理師】第四章 複盤整體管理知識架構
cin 从错误中恢复继续读入
What is the fastapi asynchronous framework? (comprehensive understanding)
How many months did you write your first SCI? (Part II)
10大主流压力/负载/性能测试工具推荐
黑客赏金猎人平台之Immunefi