Java pdfbox file not being loaded

720 Views Asked by At

I am having trouble figuring out why my file is not being loaded into a PDDocument object.

My process is as follows:

  • Open the directory as a File
  • Get an Array of Files from the directory
  • Load a file into a PDDocument.

See code below.

public class Main {

public static void main(String[] args) throws IOException {

    //open directory
    File folder = new File("pdfs");

    //Extract Files
    File[] files = folder.listFiles();

    //print out file names
    for (File file:files) {
        System.out.println(file.getName());
        System.out.println("Can read?: " + file.canRead());
        System.out.println("Can write?: " + file.canWrite());
    }


    //Load PDF
    PDDocument firstDocument = new PDDocument();

    try {
        firstDocument.load(files[0]);
    }
    finally
    {
        if (firstDocument != null) {
            firstDocument.close();

        }
    }

    System.out.println("Num Pages: " + firstDocument.getNumberOfPages());

Output:

EnterpriseArchitectInvoice.pdf
Can read?: true
Can write?: true
ooad_textbooks_invoice.pdf
Can read?: true
Can write?: true
Num Pages: 0

I can guarantee that the PDF is valid.

Thanks for the help!!!

1

There are 1 best solutions below

0
On BEST ANSWER

Instead of loading your document like this:

PDDocument firstDocument = new PDDocument();
firstDocument.load(files[0]);

do this:

PDDocument firstDocument = PDDocument.load(files[0]);

You should have seen a warning by your IDE (if it is good) that load is a static method.

enter image description here

What your code did was to show the number of pages in an empty PDDocument object.

Note that this answer applies to 2.0.* only. In 1.8.* it may work too, unless the PDF is encrypted. To cover that too, use loadNonSeq instead of load, which will decrypt too.