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

随着 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 文件,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

  • 18
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Free Spire.PDF for .NETSpire.PDF for .NET 的免费版本,无需购买即可用于个人或商业用途。使用该组件,程序员可以 在.NET 程序创建、读取、写入、编辑和操作 PDF 文档。这个控件能支持的功能十分全面,例如文档安全性设置(电子签名),提取 PDF 文本、附件、图片,PDF 合并和拆分,更新 Metadata,设置 Section,绘制图形、插入图片、表格制作和加工、导入数据等等。除此以外,Spire.PDF 还可以将 TXT 文本、图片、HTML 高质量地转换为 PDF 文件格式。 主要功能如下: 1.高质量的文档转换。Free Spire.PDF for .NET 支持 PDF 到 Word、XPS、SVG、EMF、Text 和图片(EMF、JPG、PNG、BMP、TIFF)的格式转换。也支持从 XML、HTML、RTF、XPS、Text、图片等格式生成 PDF 文档。 2.文档操作及域功能。支持合并、拆分 PDF 文档,在原有的 PDF 文档页添加覆盖页。同时,Spire.PDF 提供导入、邮戳、小册子功能,以及帮助用户从数据库读取数据并填充到域的域填写功能。 3. 安全性设置。用户可以通过设置密码和数字签名来保护 PDF 文档。用户密码和所有者密码可以确定加密的 PDF 文档的可读性、可修改性、是否可打印等有选择性的限制。与此同时,数字签名作为一个更有效的方法,可以应用于维护和对PDF文档进行身份验证。 4.数据提取。支持快速高效地从 PDF 文档提取图片、文本、PDF 分页,以及附件。 5.文件属性设置。支持对 Metadata、文件属性、页面方向、页面大小进行设置。其文件属性包括文件限制(打印、页面提取、加评论等方面的权限限制)以及文件描述属性(文件名称、作者、主题、关键字等)。使用 Spire.PDF for .NET,用户还可以根据自己阅读喜好设定默认打开页码,分页模式,缩放比例和打印缩放,等等。 6.其他功能。 支持多种语言,支持字体格式、对齐方式设置。 绘制文字,图片,图形。 支持添加图层,透明图像,Color Space,条形码到 PDF。 支持 PDF/A-1b、PDF/x1a:2001 格式。 添加梯状图形和矢量图像到指定位置。 添加并格式化表格。 插入交互元素,例如添加自定义的 Annotation、Action、JavaScript、附件、书签等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值