PdfStamper in Java Applet -
i use pdfstamper in java applet sign pdf files. problem applet every time suspends when reach line pdfstamper.close(); think problem related java applet policy have granted permissions like:
grant { permission java.security.allpermission; };
my code is:
import com.lowagie.text.documentexception; import com.lowagie.text.rectangle; import com.lowagie.text.pdf.pdfreader; import com.lowagie.text.pdf.pdfsignatureappearance; import com.lowagie.text.pdf.pdfstamper; import java.io.bufferedreader; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.security.accesscontroller; import java.security.keystore; import java.security.keystoreexception; import java.security.nosuchalgorithmexception; import java.security.privatekey; import java.security.privilegedaction; import java.security.unrecoverablekeyexception; import java.security.cert.certificate; import java.security.cert.certificateexception; import java.util.enumeration; import javax.swing.japplet; public class signapplet extends japplet { public void test() { accesscontroller.doprivileged( new privilegedaction() { @override public object run() { try { keystore keystore = keystore.getinstance( "pkcs11-custom_name" ); string pin = "custom_pin"; keystore.load( null, pin.tochararray() ); enumeration<string> aliases = keystore.aliases(); string alias = aliases.nextelement(); privatekey key = ( privatekey ) keystore.getkey( alias, pin.tochararray() ); certificate[] chain = keystore.getcertificatechain( alias ); string unsigned_pdf = "c:\\users\\user_name\\unsigned.pdf"; string signed_pdf = "c:\\users\\user_name\\signed.pdf"; pdfreader pdfreader = new pdfreader( (new file( unsigned_pdf )).getabsolutepath() ); file outputfile = new file( signed_pdf ); pdfstamper pdfstamper; pdfstamper = pdfstamper.createsignature( pdfreader, null, '\0', outputfile ); pdfsignatureappearance sap = pdfstamper.getsignatureappearance(); sap.setcrypto( key, chain, null, pdfsignatureappearance.self_signed ); sap.setreason( "reason" ); sap.setlocation( "" ); sap.setvisiblesignature( new rectangle( 10, 10, 50, 30 ), 1, null ); pdfstamper.setformflattening( true ); pdfstamper.close(); // -- applet suspends right there } catch ( exception ex ) { ex.printstacktrace(); } return null; } } ); } }
i run html that:
<applet code="signapplet.class" name="signapplet" archive="signapplet-1.0.jar, itext-2.1.7.jar" width="0" height="0"></applet> <h:form> <h:button onclick="document.signapplet.test();" value="--- test ---" /> </h:form>
keystore comes usb token.
in java debug console don't see exception. when run same source code main method (with little modification passing arguments) works well.
any idea problem?
solution: pdfstamper.close(); // -- applet suspends right there
itext pdfstamper calling api of jar bcprov*.jar. jar file signed e-certificate. need unzip jar , remove .dsa , .sf files in meta-inf. re-jar again , use e-certificate resign jar file. solve problem.
Comments
Post a Comment