PDF控件Spire.PDF for .NET【安全】演示:在 PDF 中添加或删除数字签名

news2024/9/27 19:26:47

随着 PDF 文档在商业中越来越流行,确保其真实性已成为一个关键问题。使用基于证书的签名对 PDF 进行签名可以保护内容,还可以让其他人知道谁签署或批准了该文档。在本文中,您将了解如何使用不可见或可见签名对 PDF 进行数字签名,以及如何使用Spire.PDF for .NET从 PDF 中删除数字签名。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载   Spire.PDF for java下载

安装适用于 .NET 的 Spire.PDF

首先,您需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。

PM> Install-Package Spire.PDF
向 PDF 添加不可见的数字签名

以下是使用 Spire.PDF for .NET 将不可见数字签名添加到 PDF 的步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.LoadFromFile()方法加载示例 PDF 文件。
  • 初始化PdfCertificate对象时加载 pfx 证书文件。
  • 根据证书创建PdfSignature对象。
  • 通过PdfSignature对象设置文档权限。
  • 使用PdfDocument.SaveToFile()方法将文档保存到另一个 PDF 文件。

【C#】 

using Spire.Pdf;
using Spire.Pdf.Security;

namespace AddInvisibleSignature
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Load the certificate 
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

//Create a PdfSignature object 
PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");

//Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

//Save to another PDF file 
doc.SaveToFile("InvisibleSignature.pdf");
doc.Close();
}
}
} 

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace AddInvisibleSignature
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")

'Load the certificate
Dim cert As PdfCertificate = New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")

'Create a PdfSignature object
Dim signature As PdfSignature = New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")

'Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill

'Save to another PDF file
doc.SaveToFile("InvisibleSignature.pdf")
doc.Close()
End Sub
End Class
End Namespace

C#/VB.NET:在 PDF 中添加或删除数字签名

向 PDF 添加可见的数字签名

以下是使用 Spire.PDF for .NET 将可见数字签名添加到 PDF 的步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.LoadFromFile()方法加载示例 PDF 文件。
  • 初始化PdfCertificate对象时加载 pfx 证书文件。
  • 创建一个PdfSignature对象并指定其在文档上的位置和大小。
  • 设置签名详细信息,包括日期、名称、位置、原因、手写签名图像和文档权限。
  • 使用PdfDocument.SaveToFile()方法将文档保存到另一个 PDF 文件。

【C#】

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Graphics;

namespace AddVisibleSignature
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Load the certificate
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

//Create a PdfSignature object and specify its position and size
PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 260 - 54 , 200, 260, 110);
signature.Bounds = rectangleF;
signature.Certificated = true;

//Set the graphics mode to ImageAndSignDetail
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;

//Set the signature content
signature.NameLabel = "Signer:";
signature.Name = "Gary";
signature.ContactInfoLabel = "Phone:";
signature.ContactInfo = "0123456";
signature.DateLabel = "Date:";
signature.Date = DateTime.Now;
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "USA";
signature.ReasonLabel = "Reason:";
signature.Reason = "I am the author";
signature.DistinguishedNameLabel = "DN:";
signature.DistinguishedName = signature.Certificate.IssuerName.Name;

//Set the signature image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png");

//Set the signature font
signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));

//Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

//Save to file
doc.SaveToFile("VisiableSignature.pdf");
doc.Close();
}
}
}

【VB.NET】

Imports System
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Security
Imports Spire.Pdf.Graphics

Namespace AddVisibleSignature
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Load a sample PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")

'Load the certificate
Dim cert As PdfCertificate = New PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx","e-iceblue")

'Create a PdfSignature object and specify its position and size
Dim signature As PdfSignature = New PdfSignature(doc,doc.Pages(doc.Pages.Count - 1),cert,"MySignature")
Dim rectangleF As RectangleF = New RectangleF(doc.Pages(0).ActualSize.Width - 260 - 54,200,260,110)
signature.Bounds = rectangleF
signature.Certificated = True

'Set the graphics mode to ImageAndSignDetail
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail

'Set the signature content
signature.NameLabel = "Signer:"
signature.Name = "Gary"
signature.ContactInfoLabel = "Phone:"
signature.ContactInfo = "0123456"
signature.DateLabel = "Date:"
signature.Date = DateTime.Now
signature.LocationInfoLabel = "Location:"
signature.LocationInfo = "USA"
signature.ReasonLabel = "Reason:"
signature.Reason = "I am the author"
signature.DistinguishedNameLabel = "DN:"
signature.DistinguishedName = signature.Certificate.IssuerName.Name

'Set the signature image source
signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png")

'Set the signature font
signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12f, FontStyle.Regular))

'Set the document permission to forbid changes but allow form fill
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill

'Save to file
doc.SaveToFile("VisiableSignature.pdf")
doc.Close()
End Sub
End Class
End Namespace

C#/VB.NET:在 PDF 中添加或删除数字签名

以上便是如何将加密或解密 PDF 文件,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1334262.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Java】MybatisPlus

MybatisPlus MybatisPlus是在mybatis基础上的一个增强型工具。它对mybatis的一些操作进行了简化,能够提高开发的效率。 springboot整合了mybatis之后,其实已经非常方便了,只需要导入mybatis的包后,在配置文件中编写数据源信息&a…

基于VUE3+Layui从头搭建通用后台管理系统(前端篇)十六:统计报表模块相关功能实现

一、本章内容 本章使用Echarts及DataV实现常用图表、特殊图表、地图及综合图表等图表展示功能。 1. 详细课程地址: https://edu.csdn.net/course/detail/38183 2. 源码下载地址: 点击下载 二、界面预览 三、开发视频 3.1 B站视频地址: 基于VUE3+Layui从

Adobe InDesign各版本安装指南

下载链接 https://pan.baidu.com/s/11sTpMUbQEXhyjpkBlixcLg?pwd0531 #2024版 1.鼠标右击【Ai2024(64bit)】压缩包(win11及以上系统需先点击“显示更多选项”)【解压到 Ai2024(64bit)】。 2.打开解压后的文件夹,鼠标右击【Setup】选择【以…

支付宝、学习强国小程序input、textarea数据双向绑定

前言 和 vue 的绑定有些区别,需要注意。直接 value"{{inputValue}}" 是无法双向绑定的。 正确思路 文档说的比较详细,不过没有组合使用的案例,需要自行理解。这里正确的方法是先用 value 绑定数据,再使用 onInput 事件…

docker安装及入门

笔记来自黑马课堂:【黑马程序员Docker快速入门到项目部署,MySQL部署Nginx部署docker自定义镜像DockerCompose项目实战一套搞定-哔哩哔哩】 https://b23.tv/niWEhEF 一、什么是docker: 快速构建、运行、管理应用的工具。--帮助我们快速部署提…

【数字电路】期末速通!

1. 数制及转换 常用的数制:十进制(D),二进制(B),八进制(O),十六进制(H)。 常见的码制包括以下几种: 二进制码&#xff…

不看后悔!UPS监控技术轻松搞定

在现代信息技术高度依赖的环境中,机房的稳定供电是确保业务连续性和数据安全的关键因素之一。不可预测的电力波动、突发性停电或电力故障可能对关键设备和数据造成严重影响。 因此,为了有效地监控和管理机房的电源系统,UPS监控系统变得至关重…

[Angular] 笔记 8:list/detail 页面以及@Input

1. list 页面 list/detail 是重要的 UI 设计模式。 vscode terminal 运行如下命令生成 detail 组件: PS D:\Angular\my-app> ng generate component pokemon-base/pokemon-detail --modulepokemon-base/pokemon-base.module.ts CREATE src/app/pokemon-base/p…

动态规划--使用最小花费爬楼梯

个人主页:Lei宝啊 愿所有美好如期而遇 本体题目链接https://leetcode.cn/problems/GzCJIP/ 示例图示 本题动态规划解释 动态规划,如果真要清楚理解的话,可能一开始学习不太可能,专有名词太多,我们就先简单理解。 …

自己动手做 chatgpt:seq2seq翻译模型中GRU模块的原理和数据预处理

chatgpt 是典型的 NLP 类型应用。也就是它主要于语言的处理和输出。因此它的模型设计必然借鉴了深度学习早期在语言翻译方面的算法设计。因此理解 chatgpt 我们就需要理解深度学习算法是如何处理自然语言翻译这个问题,就像学习微积分时,我们需要提前掌握…

Kafka日志文件存储

日志文件 kafka在server.properties配置文件中通过log.dir属性指定了Kafka的日志存储路径 核心文件 1. log文件 实际存储消息的日志文件, 大小固定1G(参数log.segment.bytes可配置), 写满后就会新增一个新的文件, 文件名是第一条消息的偏移量 2. index文件 以偏移量为索引…

GoogleNet网络分析与demo实例

参考自 up主的b站链接:霹雳吧啦Wz的个人空间-霹雳吧啦Wz个人主页-哔哩哔哩视频这位大佬的博客 Fun_机器学习,pytorch图像分类,工具箱-CSDN博客 1. GoogLeNet网络详解 GoogLeNet在2014年由Google团队提出(与VGG网络同年,注意GoogLeNet中的L大…

VGG网络分析与demo实例

参考自 up主的b站链接:霹雳吧啦Wz的个人空间-霹雳吧啦Wz个人主页-哔哩哔哩视频这位大佬的博客 Fun_机器学习,pytorch图像分类,工具箱-CSDN博客 VGG 在2014年由牛津大学著名研究组 VGG(Visual Geometry Group)提出,斩获该年 Imag…

【交叉编译环境】安装arm-linux交叉编译环境到虚拟机教程(简洁版本)

就是看到了好些教程有些繁琐,我就写了一个 我这个解压安装的交叉编译环境是Linaro GCC的一个版本,可以用于在x86_64的主机上编译arm-linux-gnueabihf的目标代码 步骤来了 在你的Ubuntu系统中创建一个目录,例如/usr/local/arm,然后…

day48算法训练|动态规划part09

198.打家劫舍 1. dp数组(dp table)以及下标的含义 dp[i]:考虑下标i(包括i)以内的房屋,最多可以偷窃的金额为dp[i]。 2.递推公式 决定dp[i]的因素就是第i房间偷还是不偷。 如果偷第i房间,那么…

通信原理 | 通信中有哪些量的单位是dB?

在通信领域中,分贝(dB)被用来表示各种不同的量,包括 信号强度功率电压以下是通信中常用的几种用分贝表示的量: 信号强度(Signal Strength) 通信设备发送或接收信号时,信号的强度可以用分贝来表示,通常以分贝毫瓦(dBm)为单位。 0 dBm表示1毫瓦的信号强度,负值表示…

Wafer晶圆封装工艺介绍

芯片封装的目的(The purpose of chip packaging): 芯片上的IC管芯被切割以进行管芯间连接,通过引线键合连接外部引脚,然后进行成型,以保护电子封装器件免受环境污染(水分、温度、污染物等)&…

基于ssm出租车管理系统的设计与实现论文

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本出租车管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息&…

【视觉实践】使用Mediapipe进行图像分割实践

目录 1 Mediapipe 2 Solutions 3 安装依赖库 4 实践 1 Mediapipe Mediapipe是google的一个开源项目,可以提供开源的、跨平台的常用机器学习(machine learning,ML)方案。MediaPipe是一个用于构建机器学习管道的框架,用于处理视频、音频等时间序列数据。与资源消耗型的机…

Linux开发工具——gcc篇

gcc的使用 文章目录 gcc的使用 历史遗留问题(普通用户sudo) gcc编译过程 预处理(进行宏替换) 编译(生成汇编) 汇编(生成机器可识别代码) 链接(生成可执行文件或库文件&a…