pdf-generationitextadobe-reader

Itext , pdf created couldn't be open opened by adobe reader


i wanted to create pdf with Itext , everything works until i've made the runnable jar . Adobe reader shows that message : "Adobe Reader could not open xxx.pdf because it is either not a supported file type or because the file has been damaged (for example. it was sent as an email attachment and wasn't correctly decoded)." And here is my code :

try{
         Document document = new Document();
         writer = PdfWriter.getInstance(document, new FileOutputStream(FILE) );
         document.open();
         makeBackground(document);
         makeText();
         makeTable(document);
         afterTable(document);
         document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Solution

  • There's a 90% chance that an exception is thrown in the following lines:

    makeBackground(document);
    makeText();
    makeTable(document);
    afterTable(document);
    

    This causes your application to create a PDF with a PDF header, but without a trailer (the trailer is written when you close() the document).

    If you don't tell us anything about the error message you're getting from the Exception, nobody will be able to help you.

    Possible causes:

    In any case: this isn't an iText-related problem, because you say that it works until you make the runnable jar. As it works outside the runnable jar, there probably isn't a problem with the code.

    You probably have a configuration problem: you did something wrong while creating it (for instance: you created the iText.jar yourself and you forgot to insert the .afm files). It is very hard to solve such configuration problems remotely, especially since you're not telling us which exception was thrown, which size the PDF has (could be 0), if the PDF ends with %%EOF.