Signing a pdf document

ぃ、小莉子 提交于 2020-01-04 11:39:20

问题


Currently I am upgrading my old application Which is done using itextsharp 5.0.0 to 5.4.5(latest)... But I have a problem in getting the equivalent code for

PdfSignatureAppearance.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);

Can anyone help me regarding this?

Thanks in advance...


回答1:


I sign PDF documents using 5.5 version of iTextSharp. Below is the sample code.

        // 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();


来源:https://stackoverflow.com/questions/21474649/signing-a-pdf-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!