Reputation: 2339
I am currently using the below code to extract the content and metadata of PDF files using TIKA library. Is there a way to read specific page OR limit the parsing to first few pages in TIKA?
public static void main(final String[] args) throws IOException,TikaException, SAXException {
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File("test/test.pdf"));
ParseContext pcontext = new ParseContext();
//parsing the document using PDF parser
AutoDetectParser pdfparser = new AutoDetectParser();
pdfparser.parse(inputstream, handler, metadata,pcontext);
//getting the content of the document
System.out.println("Contents of the PDF :" + handler.toString());
//getting metadata of the document
//System.out.println("Metadata of the PDF:");
String[] metadataNames = metadata.names();
System.out.println(metadata.get("xmpTPg:NPages"));
for(String name : metadataNames) {
System.out.println(name+ " : " + metadata.get(name));
}
}
Upvotes: 2
Views: 3839
Reputation: 88
TIKA doesn't really handle pages, but it does send <div><p>
before and </p></div>
after pages. You can edit your handler's startElement
and endElement
to search for these characters.
If you need more info, you can check out topchef
's answer.
https://stackoverflow.com/a/6271696/2197529
Upvotes: 1