当前,我正在升级旧的应用程序,该应用程序是使用itextsharp 5.0.0升级到5.4.5(最新)的。但是我在获取等效的代码时遇到了问题
PdfSignatureAppearance.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);
有人可以帮我吗?
提前致谢...
最佳答案
我使用5.5版本的iTextSharp签署PDF文档。
下面是示例代码。
// Set the reader (PdfReader) and output (Stream) first
PdfStamper stamper = PdfStamper.CreateSignature(reader, output, '\0');
PdfSignatureAppearance signatureAppearance = stamper.SignatureAppearance;
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
signatureAppearance.Reason = "I love signing";
signatureAppearance.LocationCaption = "";
signatureAppearance.SignatureGraphic = Image.GetInstance(this.imageFolderPath + "sign.png");
signatureAppearance.SetVisibleSignature(
new Rectangle(100, 100, 300, 200),
reader.NumberOfPages,
"Signature");
// Get certificate from store, here I am reading file
X509Certificate2 cert = new X509Certificate2(certFile, certPassword);
var keyPair = DotNetUtils.GetKeyPair(cert.PrivateKey).Private;
BcX509.X509Certificate bcCert = DotNetUtils.FromX509Certificate(cert);
var chain = new List<BcX509.X509Certificate> { bcCert };
IExternalSignature signature = new PrivateKeySignature(keyPair, "SHA-256");
MakeSignature.SignDetached(signatureAppearance, signature, chain, null, null, null, 0, CryptoStandard.CMS);
stamper.Close();