【发布时间】:2016-09-04 14:06:40
【问题描述】:
我有一个 PDF 文件,我想检查它是否有数字签名。使用 iTextsharp,C# 中的代码。
【问题讨论】:
-
您使用的是什么代码,您遇到了什么具体问题?
标签: c# asp.net asp.net-mvc-4 itext sign
我有一个 PDF 文件,我想检查它是否有数字签名。使用 iTextsharp,C# 中的代码。
【问题讨论】:
标签: c# asp.net asp.net-mvc-4 itext sign
我建议你看看官方的例子。
它们包含例如一个示例“SignatureInfo”,它输出嵌入在 PDF 中的所有签名的多个信息项;因此,它们特别确定文件是否已签名。
如果您使用的是 iTextSharp 5.5.x,那么您的关键代码就是这个
public void InspectSignatures(String path) {
Console.WriteLine(path);
PdfReader reader = new PdfReader(path);
AcroFields fields = reader.AcroFields;
List<String> names = fields.GetSignatureNames();
SignaturePermissions perms = null;
foreach (String name in names) {
Console.WriteLine("===== " + name + " =====");
perms = InspectSignature(fields, name, perms);
}
Console.WriteLine();
}
(来自 iTextSharp 示例 C5_02_SignatureInfo.cs)
如您所见,AcroFields.GetSignatureNames() 方法为您获取所有签名签名字段的名称。如果该列表非空,则 PDF 已签名。
如果您使用 iText 7 for .Net,您的关键代码如下:
public virtual void InspectSignatures(String path)
{
// System.out.println(path);
PdfDocument pdfDoc = new PdfDocument(new PdfReader(path));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false);
SignaturePermissions perms = null;
SignatureUtil signUtil = new SignatureUtil(pdfDoc);
IList<String> names = signUtil.GetSignatureNames();
foreach (String name in names)
{
System.Console.Out.WriteLine("===== " + name + " =====");
perms = InspectSignature(pdfDoc, signUtil, form, name, perms);
}
System.Console.Out.WriteLine();
}
(来自 iText 7 for .Net 示例 C5_02_SignatureInfo.cs)
如您所见,SignatureUtil.GetSignatureNames() 方法为您获取所有签名签名字段的名称。如果该列表非空,则 PDF 已签名。
顺便说一下,由于您没有进一步说明,我假设您的意思是常规集成 PDF 签名,特别是分离签名和 XFA 签名。
【讨论】: