Hi, 

I've got a problem using iText to digitally sign a PDF.  The Groovy script 
below works fine with a test PDF I created in Acrobat Pro 7.0.  But when I 
run it on a PDF produced by my production document system (Oracle 
Documaker), I get this exception: 

java.lang.ArrayIndexOutOfBoundsException: 2 
        at 
com.itextpdf.text.pdf.PdfEncryption.createInfoId(PdfEncryption.java:401) 

I'm using iText 5.0.2, JDK 1.6, and Groovy 1.5.6. 

Both PDFs are encrypted before my script begins - with printing and 
signing allowed, but changing of the document prohibited.  The Acrobat 7 
PDF is PDF Version 1.6; the Documaker PDF is PDF Version 1.4.  Both are 
encrypted with 128-bit RC4.  I don't know where to look for any other 
material differences between the two files. 

Does anyone have any thoughts on why iText throws an exception when trying 
to sign the Documaker-generated PDFs? 


import java.security.KeyStore 
import java.security.PrivateKey 
import java.security.cert.Certificate 
import com.itextpdf.text.pdf.PdfReader 
import com.itextpdf.text.pdf.PdfStamper 
import com.itextpdf.text.pdf.PdfWriter 
import com.itextpdf.text.pdf.PdfSignatureAppearance 
import com.itextpdf.text.pdf.PdfName 
import com.itextpdf.text.Image 
import com.itextpdf.text.Rectangle 

static String sign( 
        String keyFile, 
        String keyFilePassword, 
        String inFile, 
        String ownerPassword, 
        String reason, 
        String location, 
        String signer, 
        String sigFieldName, 
        String outFile 
) { 
        try { 
                KeyStore ks = KeyStore.getInstance("pkcs12") 
                ks.load(new FileInputStream(keyFile), 
keyFilePassword.toCharArray()) 
                String alias = (String)ks.aliases().nextElement() 
                PrivateKey key = (PrivateKey)ks.getKey(alias, 
keyFilePassword.toCharArray()) 
                Certificate[] chain = ks.getCertificateChain(alias) 
                PdfReader reader = new PdfReader(inFile, 
ownerPassword.getBytes()) 
                FileOutputStream fout = new FileOutputStream(outFile) 
                // Groovy thinks '\0' is a GString, so we have to be 
explicit and force it to char. 
                PdfStamper stp = PdfStamper.createSignature(reader, fout, 
'\0'.toCharacter().charValue(), null, true) 
                stp.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_6) 
                PdfSignatureAppearance sap = stp.getSignatureAppearance() 
                sap.setCrypto(key, chain, null, 
PdfSignatureAppearance.WINCER_SIGNED) 
                sap.setReason(reason) 
                sap.setLocation(location) 
                sap.setLayer2Text(signer) 
                sap.setVisibleSignature(sigFieldName) 
                stp.close() 
                return outFile 
        } catch (Throwable t) { 
                println t 
        } 
} 

String outfile = sign( 
        "C:/certs/eAppTest_20110429.pfx", 
        [redacted], 
        "C:/certs/72567C71-D0AD-ADAB-E6EE-D464F714650C-0-IDS-1-BLP.PDF", 
        [redacted], 
        "reason", 
        "location", 
        "Foo Bar", 
        "insuredSignature", 
 "C:/certs/72567C71-D0AD-ADAB-E6EE-D464F714650C-0-IDS-1-BLP-signed.PDF" 
) 

println "Signed ${outfile}"
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to