[英]How to check if a PDF file is signed or not using iTextsharp in C#?
我有一個PDF文件,我想檢查它是否由數字簽名簽名。 使用iTextsharp,這是C#中的代碼。
我建議您看一下官方示例。
它們包含一個示例“ 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用於.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();
}
(來自.Net的iText 7示例C5_02_SignatureInfo.cs )
如您所見,方法SignatureUtil.GetSignatureNames()
您獲取所有已簽名簽名字段的名稱。 如果該列表非空,則對PDF簽名。
順便說一句,由於您沒有進一步指定,我假設您是指常規的集成PDF簽名,尤其是分離簽名和XFA簽名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.