How to read PDF files using Java?

yohan.jayarathna picture yohan.jayarathna · Jan 24, 2011 · Viewed 201.7k times · Source

I want to read some text data from a PDF file using Java. How can I do that?

Answer

Michael Berry picture Michael Berry · Jan 24, 2011

PDFBox is the best library I've found for this purpose, it's comprehensive and really quite easy to use if you're just doing basic text extraction. Examples can be found here.

It explains it on the page, but one thing to watch out for is that the start and end indexes when using setStartPage() and setEndPage() are both inclusive. I skipped over that explanation first time round and then it took me a while to realise why I was getting more than one page back with each call!

Itext is another alternative that also works with C#, though I've personally never used it. It's more low level than PDFBox, so less suited to the job if all you need is basic text extraction.