I want to extract different content from a PDF file in Java:
- The complete visible text
- images
- links
Is it also possible to get the following?
- document meta tags like title, description or author
- only headlines
- input elements if the document contains a form
I do not need to manipulate or render PDF files. Which library would be the best fit for that kind of purpose?
UPDATE
OK, I tried PDFBox:
Document luceneDocument = LucenePDFDocument.getDocument(new File(path));
Field contents = luceneDocument.getField("contents");
System.out.println(contents.stringValue());
But the output is null. The field "summary" is OK though.
The next snippet works fine.
PDDocument doc = PDDocument.load(path);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(doc);
System.out.println(text);
doc.close();
But then, I have no clue how to extract the images, links, etc.
UPDATE 2
I found an example how to extract the images, but I still got no answer on how to extract:
- links
- document meta tags like title, description or author
- only headlines
- input elements if the document contains a form
iText is my PDF tool of choice these days.
"Visible" is a tough one. You can parse out all the parsable text with the com.itextpdf.text.pdf.parse package's classes... but those classes don't know about CLIPPING. You can constrain the parser to the page size easily enough.
You'd actually need the override that takes a TextExtractionStrategy, the filtered strategy. It gets interesting fairly quickly, but I think you can get everything you want here "out of the box".
Yep, via the same package classes. Image listeners aren't as well supported as text listeners, but do exist.
Yes. Links are "annotations" to various PDF pages. Finding them is a simple matter of looping through each page's "annotations array" and picking out the link annotations.
You can find the PDF Spec here.
Definitely. For either XFA (LiveCycle Designer) or the older-tech "AcroForm" forms, iText can find all the fields, and their values.
Mutli-select lists wouldn't be handled all that well. You'll get a blank space after the colon for empty text fields and for buttons. None too informative... but that'll get you started.
Pretty trivial. Yes.
In addition to the basic author/title/etc, there's a fairly involved XML schema you can access via
reader.getMetadata()
.A
TextRenderFilter
can ignore text based on whatever criteria you wish. Font size sounds about right based on your comment.Yes Alp, iText does offer the functionality you mentioned.
Use
com.itextpdf.text.pdf.PdfReader;
class.Apache comes to the rescue, once again.
You can also use JPedal for all these extraction tasks.
Most of this you can do with our PDF Library extended edition as well.
Whichever solution you go for, bear in mind that for certain PDF documents, text extraction is impossible due to the way the PDF is constructed (the glyphs on the page sometimes don't have any semantic meaning associated with them).
The quick way to check this is to open the document in Acrobat and try copying/pasting the text. If it comes up as gibberish there, chances are it will come up as gibberish in any other PDF extractor.