Hello,
I'm using iText 4.1.6 to sign PDF documents.

What I try to achieve is to certificate document and then apply approval
signature.
I have done first step, certified document and added empty invisible
signature field.

        public void SignFirstTime()
        {
            var reader = new PdfReader("d:\\NotSigned.pdf");
            var writer = new FileStream("d:\\Signed.pdf", FileMode.Create,
FileAccess.Write);
            PdfStamper st = PdfStamper.CreateSignature(reader, writer,
'\0');
        
            PdfSignatureAppearance sap = st.SignatureAppearance;

            sap.SetCrypto(Key, GetCertificatesChain(), null,
PdfName.ADOBE_PPKLITE);
            sap.SetVisibleSignature(new Rectangle(0, 0, 60, 40), 1,
"CertificationSignature");
            sap.Reason = "Test";
            sap.CertificationLevel =
PdfSignatureAppearance.CERTIFIED_FORM_FILLING;
            //Adding invisible empty signature field to be signed later
            st.AddSignature("ApprovalSignature", 1, 0, 0, 0, 0);
            
            var chain = GetCertificatesChain();

            reader.Appendable = true;

            var dic = new PdfSignature(PdfName.ADOBE_PPKLITE
,PdfName.ADBE_PKCS7_DETACHED);
            dic.Reason = sap.Reason;
            dic.Contact = sap.Contact;
            dic.Location = sap.Location;
            dic.Date = new PdfDate(sap.SignDate);
            sap.CryptoDictionary = dic;

            var exc = new Hashtable();
            exc[PdfName.CONTENTS] = ContentEstimated * 2 + 2;
            sap.PreClose(exc);

            PdfPKCS7 sgn = new PdfPKCS7(Key, chain, null, "SHA-256", false);
            IDigest messageDigest = DigestUtilities.GetDigest("SHA-256");
            Stream data = sap.RangeStream;
            byte[] buf = new byte[ContentEstimated];
            int n;
            while ((n = data.Read(buf, 0, buf.Length)) > 0)
            {
                messageDigest.BlockUpdate(buf, 0, n);
            }
            byte[] hash = new byte[messageDigest.GetDigestSize()];
            messageDigest.DoFinal(hash, 0);
            DateTime cal = DateTime.Now;

            byte[] sh = sgn.GetAuthenticatedAttributeBytes(hash, cal, null);
            sgn.Update(sh, 0, sh.Length);

            byte[] encodedSig = sgn.GetEncodedPKCS7(hash, cal, null, null);
            if (ContentEstimated + 2 < encodedSig.Length)
                throw new Exception("Not enough space");

            var paddedSig = new byte[ContentEstimated];
            Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length);

            var dic2 = new PdfDictionary();
            dic2.Put(PdfName.CONTENTS, new
PdfString(paddedSig).SetHexWriting(true));
            sap.Close(dic2);
        }

Then I try to sign this empty invisible signature field:

        public void SignSecondTime()
        {
            var reader = new PdfReader("d:\\Signed.pdf");
            var writer = new FileStream("d:\\Signed2.pdf", FileMode.Create,
FileAccess.Write);
            PdfStamper st = PdfStamper.CreateSignature(reader, writer, '\0',
null, true);

            PdfSignatureAppearance sap = st.SignatureAppearance;

            sap.SetCrypto(Key, GetCertificatesChain(), null,
PdfName.ADOBE_PPKLITE);
            sap.SetVisibleSignature("ApprovalSignature");
            sap.Reason = "Test";
            sap.CertificationLevel = PdfSignatureAppearance.NOT_CERTIFIED;

            var chain = GetCertificatesChain();

            reader.Appendable = true;

            var dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
PdfName.ADBE_PKCS7_DETACHED);
            dic.Reason = sap.Reason;
            dic.Contact = sap.Contact;
            dic.Location = sap.Location;
            dic.Date = new PdfDate(sap.SignDate);
            sap.CryptoDictionary = dic;

            var exc = new Hashtable();
            exc[PdfName.CONTENTS] = ContentEstimated * 2 + 2;
            sap.PreClose(exc);

            PdfPKCS7 sgn = new PdfPKCS7(Key, chain, null, "SHA-256", false);
            IDigest messageDigest = DigestUtilities.GetDigest("SHA-256");
            Stream data = sap.RangeStream;
            byte[] buf = new byte[ContentEstimated];
            int n;
            while ((n = data.Read(buf, 0, buf.Length)) > 0)
            {
                messageDigest.BlockUpdate(buf, 0, n);
            }
            byte[] hash = new byte[messageDigest.GetDigestSize()];
            messageDigest.DoFinal(hash, 0);
            DateTime cal = DateTime.Now;

            byte[] sh = sgn.GetAuthenticatedAttributeBytes(hash, cal, null);
            sgn.Update(sh, 0, sh.Length);

            byte[] encodedSig = sgn.GetEncodedPKCS7(hash, cal, null, null);
            if (ContentEstimated + 2 < encodedSig.Length)
                throw new Exception("Not enough space");

            var paddedSig = new byte[ContentEstimated];
            Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length);

            var dic2 = new PdfDictionary();
            dic2.Put(PdfName.CONTENTS, new
PdfString(paddedSig).SetHexWriting(true));
            sap.Close(dic2);
        }

but it doesn't sign it. If I add visible empty signature field, everything
works fine. I managed to sign invisible signature field only with Acrobat XI
Pro. Thanks.



--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Signing-invisible-signature-field-tp4659024.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to