2

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...

Andrea
  • 11,801
  • 17
  • 65
  • 72
Posting Pro
  • 43
  • 2
  • 7
  • The signing API (at least the top level you are expected to use) has undergone quite some changes. You might want to read [Digital Signatures for PDF documents](http://itextpdf.com/book/digitalsignatures), *A White Paper by Bruno Lowagie (iText Software)* on signing with iText(Sharp). The code samples in it are in Java but C# versions also are available. – mkl Jan 31 '14 at 07:44
  • Can u please provide me the c# link? – Posting Pro Jan 31 '14 at 10:03
  • They can at least be found in the subversion repository in [tutorial](http://svn.code.sf.net/p/itextsharp/code/tutorial); I don't know, though, whether there is some beautified version anywhere. – mkl Jan 31 '14 at 10:16
  • No, a series of examples were ported to C#, but those C# versions are only available on SourceForge. – Bruno Lowagie Jan 31 '14 at 11:11
  • @BrunoLowagie I gone through your example. But the problem is with the extraction of details like private key from a pfx file. So I am undergoing with a distraction of topic. Is there a specific sample for my problem? – Posting Pro Feb 03 '14 at 06:49
  • That's actually a different question (not related to iText). Post another question with tags PKI, PKCS#12, private key, C#,... That stuff is a prerequisite when applying a digital signature. I know how it's done in Java, but you'll have to search for the corresponding C# mechanism. – Bruno Lowagie Feb 03 '14 at 07:49

1 Answers1

4

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();
Sajad Deyargaroo
  • 1,149
  • 1
  • 7
  • 20