Hi,

Am 26.11.2014 um 16:25 schrieb Kevin Ternes:
I encountered NullPointerExceptions when attempting to deal with some PDF files.

    RandomAccessFile raFile = new RandomAccessFile(inPdfFile, "r");
    COSDocument cosDocument = new COSDocument(raFile);
    PDDocument document = new PDDocument(cosDocument);
    int documentNumberOfPages = document.getNumberOfPages();
I hope you didn't found that somewhere on the internet as a sample on how to load a pdf using PDFBox. Try something like this:

PDDocument document = PDDocument.loadNonSeq(inPdfFile, null);



This results in:
java.lang.NullPointerException: null
         at 
org.apache.pdfbox.pdmodel.PDDocument.getDocumentCatalog(PDDocument.java:763) 
~[pdfbox-1.8.7.jar:na]
         at 
org.apache.pdfbox.pdmodel.PDDocument.getNumberOfPages(PDDocument.java:1423) 
~[pdfbox-1.8.7.jar:na]
         at 
com.pgac.jobs.mdr.agentsite.AgencySortHandler.handleSpoolFile(AgencySortHandler.java:891)
 [AgencySortHandler.class:na]

PDDocument.java:763 is here:
762            COSDictionary trailer = document.getTrailer();
763            COSBase dictionary = trailer.getDictionaryObject( COSName.ROOT );


This does not work either:
    PDDocumentCatalog documentCatalog = new PDDocumentCatalog(document);
    List<PDPage> pdPageList = documentCatalog.getAllPages();

So apparently my PDF files are NON-conformant and have NULL for a Trailer.

Is there another way to get the number of pages in this case?

Also, rather than throw a NPE, wouldn't it be a good idea to throw an Exception 
with a message indicating what is wrong with the input?



BR
Andreas Lehmkühler

Reply via email to