简体   繁体   中英

How to read first few pages of a PDF file using TIKA

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));
      }
   }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM