Here it is my code. I can't understand this behaviour:
public static String signPdf(String alias,String reason, String location, String signatureFieldName, String fileContentBase64, String role ) throws IOException, DocumentException, GeneralSecurityException, InvalidNameException { DebugTraces.trace("Init method signPdf", SignPDF.class.getName()); String fileSignedContentBase64 = null; KeyStore ks = getKeyStore("Windows-MY"); boolean review = false; //ks.load(new FileInputStream(path), keystore_password.toCharArray()); PrivateKey pk = (PrivateKey)ks.getKey(alias, null); Certificate[] chain = ks.getCertificateChain(alias); Certificate cert= ks.getCertificate(alias); X509Certificate cert2 = (X509Certificate) cert; String CNCert=""; String dn = cert2.getSubjectX500Principal().getName(); //To take the signer Name LdapName ldapDN = new LdapName(dn); Rdn rdn; for(int i=0;i< ldapDN.getRdns().size();i++) { rdn = ldapDN.getRdn(i); if(rdn.getType().equals("CN")) { CNCert=rdn.getValue().toString(); } } // reader and stamper byte[] bytePdfFile = Base64Utils.base64Decode(fileContentBase64); int numSig = 0; try { numSig = ValidatePdf.getNumberSignatures(bytePdfFile); } catch(Exception en) { numSig = 0; } if (numSig > 0) { review = true; } DebugTraces.trace("Number of Signatures: "+numSig, SignPDF.class.getName()); PdfReader reader = new PdfReader(bytePdfFile); ByteArrayOutputStream bout = new ByteArrayOutputStream(); PdfStamper stamper = PdfStamper.createSignature(reader, bout, '\0',null,true); stamper.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.STRENGTH128BITS); // appearance PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); appearance.setCrypto(pk, chain, null, PdfSignatureAppearance.WINCER_SIGNED); appearance.setVisibleSignature(signatureFieldName); appearance.setReason(reason); appearance.setLocation(location); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); //sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String res= sdf.format(new Date()); res= res.substring(0, 23)+":"+res.substring(23); appearance.setLayer2Font(new Font(Font.HELVETICA, 6, Font.NORMAL)); appearance.setLayer2Text("Digitally signed by "+CNCert+"\nSigned as: "+role+"\nDate: " + res+"\nReason: "+reason+"\nLocation: "+location); /*Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); cal.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = new Date(System.currentTimeMillis()); cal.setTime(date); appearance.setSignDate(cal);*/ if(!review) { appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_FORM_FILLI NG); } else { appearance.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED); } appearance.setExternalDigest(null, new byte[20], null); // signature appearance.preClose(); MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); byte buf[] = new byte[8192]; int n; InputStream inp = appearance.getRangeStream(); while((n=inp.read(buf))>0) { messageDigest.update(buf,0,n); } byte hash[] = messageDigest.digest(); PdfSigGenericPKCS sg = appearance.getSigStandard(); //PdfDate dat= new PdfDate(cal); //sg.setDate(dat); PdfLiteral slit = (PdfLiteral)sg.get(PdfName.CONTENTS); byte[] outc = new byte[(slit.getPosLength() - 2) / 2]; PdfPKCS7 sig = sg.getSigner(); sig.setExternalDigest(null, hash, null); //sig.setSignDate(cal); PdfDictionary dic = new PdfDictionary(); byte[] ssig = sig.getEncodedPKCS7(); System.arraycopy(ssig, 0, outc, 0, ssig.length); dic.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true)); appearance.close(dic); fileSignedContentBase64 = Base64Utils.base64Encode(bout.toByteArray()); DebugTraces.trace("End method signPdf", SignPDF.class.getName()); return fileSignedContentBase64; } -----Mensaje original----- De: mkl [mailto:m...@wir-sind-cool.org] Enviado el: viernes, 08 de noviembre de 2013 22:04 Para: itext-questions@lists.sourceforge.net Asunto: [iText-questions] [SPAM] Re: [SPAM] Re: Problem with E-signature and iText Aritz, Aritz_85 wrote > Tomorrow I'll try to send you my code. Please also supply a sample PDF illustrating the issue. I have to admit, though, that I only have Acrobat 9.5 at my hands currently and, therefore, possibly am not able to reproduce the issue. Regards, Michael -- View this message in context: http://itext-general.2136553.n4.nabble.com/Problem-with-E-signature-and-iTex t-tp4659420p4659425.html Sent from the iText - General mailing list archive at Nabble.com. ---------------------------------------------------------------------------- -- November Webinars for C, C++, Fortran Developers Accelerate application performance with scalable programming models. Explore techniques for threading, error checking, porting, and tuning. Get the most from the latest Intel processors and coprocessors. See abstracts and register http://pubads.g.doubleclick.net/gampad/clk?id=60136231&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 ------------------------------------------------------------------------------ DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access Free app hosting. Or install the open source package on any LAMP server. Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native! http://pubads.g.doubleclick.net/gampad/clk?id=63469471&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