Search code examples
javapdfadobeitext

iText Error: java.io.IOException: trailer not found


I'm creating a web application which will fill a PDF form using iText. To create the PDF forms I'm first using Microsoft Word to create a template, saving it, then opening that file in Adobe Acrobat Xi Pro, adding my form fields, and saving it as a PDF. The problem is the PDF is not saving with a trailer so when I execute this:

PdfReader reader = new PdfReader(templateName);

It throws an exception "java.io.IOException: trailer not found". I know I can read a PDF if it has a trailer because I've tried reading other PDFs. So it appears the issue is that Acrobat is not adding a trailer to my PDF. Even if I try creating a PDF form from scratch in Acrobat it is not saved with a trailer.

Has anyone else run into this problem? Is there some setting in Acrobat that will add the trailer? Is there a way to get iText to read it without the trailer?

====UPDATE====

I must have had an old version of iText because when I downloaded the latest version I was able to read my PDF file. However after reading the file and stamping it I got an exception closing the stamper. The code looks like this:

PdfReader reader = new PdfReader(templateName);
FileOutputStream os = new FileOutputStream(outputPath);
PdfStamper stamper = new PdfStamper(reader, os);
AcroFields acroFields = stamper.getAcroFields();

List<String> fields = getFieldNames(getContextCd());
for (String field : fields) {
    acroFields.setField (field, StringUtil.checkEmpty(request.getParameter(field)));
}

stamper.setFormFlattening(true);
stamper.close();

The error I got was:

java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V at com.itextpdf.xmp.impl.XMPMetaParser.createDocumentBuilderFactory(XMPMetaParser.java:423) at com.itextpdf.xmp.impl.XMPMetaParser.(XMPMetaParser.java:71) at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:167) at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:153) at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:337) at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:208)

The only jar file I added to my classpath is itextpdf-5.5.2.jar. Do I need any of the other jars?


Solution

  • My Solution will work... Assumption 1: the pdf is missing trailer, it is placed under resources/xxx and after build this is moved to classess/xxx Assumption 2: you are trying to read the pdf from classpath and trying to make a PdfReader object from this pdf file path.

    If above assumptions are right, below is the solution followed by reasoning: go to your pom file or what ever build configuration file you have, and change the setting to exclude .pdf files from being filtered during build. we want the pdf to move from resources/xxx to classess/xxx without any manipulation by build activities. something like below:

    <build>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    **<exclude>**/*.pdf</exclude>**
                </excludes>
              </resource>
            </resources>
    

    Explanation needed? Ok. When resources are being moved from resources/xxx to classess/xxx, no matter what kind of file they are, they are all interpreted by build process. while doing so for the PDF, trailer and EOF tags are expected. If not, the build tool adds extra characters to pdf content to report the problem by any code trying to use this PDF. If we skip the filtering of PDF from build activity, the same pdf will work even if the trailer is missing.

    Hope this helps!