签署pdf文件

时间:2014-01-31 07:28:11

标签: c# key itextsharp pki pkcs#12

目前我正在升级我的旧应用程序,这是使用itextsharp 5.0.0到5.4.5(最新)完成的......但我在获取

的等效代码时遇到问题
PdfSignatureAppearance.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);

有人可以帮我解决这个问题吗?

提前致谢...

1 个答案:

答案 0 :(得分:4)

我使用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();