will
will

Reputation: 59

Undefined constructer in code employing the use of PDFbox repository

This is my first time using a Maven repository so apologies if its a simple resolution.

My code is as follows:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class application {

public static void main(String args[]) {
    PDFTextStripper pdfStripper = null;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;
    File file = new File("/Users/Desktop/Corporate reports/previous 'fetch' items/ARM2009.pdf");
    try {
        PDFParser parser = new PDFParser(new FileInputStream(file));
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(5);
        String parsedText = pdfStripper.getText(pdDoc);
        System.out.println(parsedText);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Failed to parse : " +file);
    } 
}

}

Essentially, the 19th line where it say:

PDFParser parser = new PDFParser(new FileInputStream(file));

is giving an error at compile time. It is saying:

The constructor PDFParser(FileInputStream) is undefined

I am not sure how to handle this. My IDE recommends cast the argument to RandomAccessRead but this just ends up with a different error at run time

Please help thank you.

Upvotes: 0

Views: 698

Answers (1)

borowis
borowis

Reputation: 1235

If you compare javadocs for the pdfparser in pdfbox v2 vs v1.8, you will notice that the constructor definition has changed from

PDFParser(InputStream input)

to

PDFParser(RandomAccessRead source)

So please make sure you reference the correct version from maven. If you plan to stick to version 2, make sure to use something like RandomAccessFile, not a FileInputStream.

Upvotes: 1

Related Questions