3

I'm currently developing an ERP application which uses iText for creating and signing PDF files. The idea is that the app can generate a PDF file with a bill, then use PdfStamper or any other class to sign it with a digital signature. Here's my code:

CREATING AND EDITING THE BILL

File f1 = null;
f1 = new File("myFilePath");
f1.delete();
if ((f1 != null) && (f1.createNewFile()))
{
    //Here I call the procedure that creates the bill
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f1));
    PdfFormField field = PdfFormField.createSignature(writer);

    PdfSigner pdfSigner = new PdfSigner();
    pdfSigner.setFileIn(f1.getAbsolutePath());
    pdfSigner.setFileKey("myKeyPath.pfx");
    pdfSigner.setFileKeyPassword("myPassword");
    pdfSigner.setFileOut("myOutputPath");

    if (pdfGenerator.factura(writer, pdfSigner, document))
    {   
        //Here I show the File to the user
    }
}
else
{
    //Here I show an error
}

PROCEDURE "factura"

public boolean factura (PdfWriter writer, PdfSigner signer, Document document) throws NullPointerException
{
    try
    {
        //Here I set a PdfPageEvent before attaching it to the PdfWriter
        writer.setPageEvent(myPdfPageEvent);
        document.open();
        //Here I manipulate the Document to generate the bill
        signer.signPdf();
        document.close();
        return true;
    }
    //catch 4 or 5 different types of exceptions and return false if needed
}

CLASS PdfSigner

public class PdfSigner
{

private String fileKey = null;
private String fileKeyPassword = null;
private String fileIn = null;
private String fileOut = null;

public PdfSigner() {}

public boolean signPdf() throws IOException, DocumentException, Exception
{

    if (fileKey == null || fileKeyPassword == null || fileIn == null || fileOut == null) return false;

    try
    {
        KeyStore ks = KeyStore.getInstance("pkcs12");
        ks.load(new FileInputStream(fileKey), fileKeyPassword.toCharArray());
        String alias = (String) ks.aliases().nextElement();
        PrivateKey key = (PrivateKey) ks.getKey(alias, fileKeyPassword.toCharArray());
        Certificate[] chain = ks.getCertificateChain(alias);
        //BOOOOOM!
        PdfReader pdfReader = new PdfReader((new File(fileIn)).getAbsolutePath());
        FileOutputStream outputFile = new FileOutputStream(fileOut);
        PdfStamper pdfStamper = PdfStamper.createSignature(pdfReader, outputFile, '?');
        PdfSignatureAppearance sap = pdfStamper.getSignatureAppearance();
        sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
        sap.setVisibleSignature(new Rectangle(10, 10, 50, 30), 1, "sign_rbl");
        pdfStamper.close();
        return true;
    }
    catch (Exception key)
    {
        throw new Exception(key);
    }
}

//getters and setters

}

Well, this is wrong, but I don't know where does it fail. If I try to run it, it usually throw an exception in the line that I've marked with a "BOOOOM!", before setting the PdfReader. But if I try to sign it outside the procedure "factura", after closing the document, the exception is usually thrown almost in the last line, when I close the PdfStamper. In both cases, the cause is always the same: "PDF header signature not found."

Anyone has an idea of what's happening? I am 99% sure the paths I'm giving to the program are right, and the digital signature and password are also right...

Thanks

PS: I swear I've tried to find a solution among the multiple answers in this page, but none of them proved to be of any use to me

user1892498
  • 31
  • 1
  • 2
  • 1
    Solved. It was something about some library (bouncy castle) and the fact that I was opening the wrong file. In the end, the signature is done after the document is closed, but it works perfectly – user1892498 Dec 11 '12 at 18:01
  • 1
    Note that you're using SELF_SIGNED. As a result, your signature won't be future proof. Please read this white paper for more info: http://itextpdf.com/book/digitalsignatures – Bruno Lowagie Dec 12 '12 at 08:04
  • but WINCER_SIGNED is also deprecated... what should we use in the latest free version of iText? – marcolopes Mar 31 '13 at 04:16

0 Answers0