Itextsharp数字签名并在PDF上显示证书的信息(主题和发行者名称)作为附件图像 [英] Itextsharp Digital sign and show info(subject and issuername) of certificate on PDF as attached image
问题描述
我正在一个项目中使用iTextSharp对PDF文件进行数字签名,这是可行的,但是需要直接在PDF上显示证书的信息(请参阅附图),如何执行或设置iTextSharp的任何参数?
I am working on one project using iTextSharp to digitally sign PDF file, it's workable, but need directly show the certificate's info on PDF (Please refer to attached image), how to do or set any parameter of iTextSharp?
public static X509Certificate2 cert;
//Sign with certificate selection in the windows certificate store
public static void Sign(string pdfFile, string outPdfFile){
Program.WriteLog("Signing Digital Certificate");
string IssuerName = null;
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
IssuerName = Properties.Settings.Default.IssuerName;
if (IssuerName.Length > 0)
cert = store.Certificates.Find(X509FindType.FindByIssuerName, IssuerName, false)[0];
if (cert == null)
{
//manually chose the certificate in the store
X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
if (sel.Count > 0)
cert = sel[0];
else
{
Console.WriteLine("Certificate not found");
return;
}
}
PdfReader reader = new PdfReader(pdfFile); // source pdf file
FileStream os = new FileStream(outPdfFile, FileMode.Create); //the output pdf file
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
stamper.SetEncryption(PdfWriter.STRENGTH128BITS, "", null, PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
try
{
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1");
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
//here set signatureAppearance at your will
appearance.Reason = Properties.Settings.Default.DigitalSignReason;
appearance.Location = Properties.Settings.Default.DigitalSignLocation;
appearance.Contact = Properties.Settings.Default.DigitalSignContact;
if (Properties.Settings.Default.DigitalSignAppearance == 1)
{
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, 60), 1, "Signed");
}
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
MakeSignature.SignDetached(appearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
//MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES);
}catch(Exception e){
Console.WriteLine(e.Message, 1);
File.Delete(outPdfFile);
}
finally
{
if (reader != null)
reader.Close();
if (stamper != null)
stamper.Close();
if (os != null)
os.Close();
}
}
推荐答案
根据您的屏幕快照,您需要一种特殊的签名,即认证签名.您可以通过添加
According to your screen shot you want a special kind of signature, a certification signature. You can create such a signature by adding
appearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_FORM_FILLING_AND_ANNOTATIONS;
您的代码.另外,要允许较少的更改,请使用CERTIFIED_FORM_FILLING
或CERTIFIED_NO_CHANGES_ALLOWED
.
to your code. Alternatively, to allow less changes, use CERTIFIED_FORM_FILLING
or CERTIFIED_NO_CHANGES_ALLOWED
.
这篇关于Itextsharp数字签名并在PDF上显示证书的信息(主题和发行者名称)作为附件图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!