1

How to extract documents like (pdf,docx,doc,odt) without headers and footer using apache tika.

Ateeb Khan
  • 11
  • 1
  • 2
  • Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – P.J.Meisch May 16 '17 at 06:42
  • look http://stackoverflow.com/questions/16862346/ignoring-header-footer-text-when-using-tika and https://coderanch.com/t/679868/Apache-Tika-Skipping-Header-footer . – Nicomedes E. May 16 '17 at 10:33
  • Grab as XHTML, strip out the Header and Footer divs, then downmix to plain text if required? – Gagravarr May 16 '17 at 20:50
  • look http://stackoverflow.com/questions/42589076/apache-tika-how-to-extract-html-body-with-out-header-and-footer-content – User4567 May 18 '17 at 10:23

2 Answers2

1

I tested this code with all the file formats, some are parsing well(pdf and html) and not working for doc,docx,xlsx,xls formats

import org.apache.tika.exception.TikaException;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.html.BoilerpipeContentHandler;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.apache.tika.metadata.Metadata;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;   

public class NewtikaXpath {
    public static void main(String args[]) throws IOException, SAXException, TikaException {
        AutoDetectParser parser = new AutoDetectParser();
        ContentHandler textHandler = new BodyContentHandler();
        Metadata xmetadata = new Metadata();
        try  (InputStream stream = TikaInputStream.get(new URL("your favourite url"))){
            parser.parse(stream, new BoilerpipeContentHandler(textHandler), xmetadata);
            System.out.println("text:\n" + textHandler.toString());
        }
    }

}
User4567
  • 1,005
  • 1
  • 12
  • 27
1

You can do it pro-grammatically. Here is how and it's working for all tika supported documents including docx, pptx, odt pdf

   ParseContext parseContext = new ParseContext();
AutoDetectParser parser = new AutoDetectParser();
ContentHandler contentHandler = new BodyContentHandler();
inputStream = new BufferedInputStream(new FileInputStream(inputFileName));
Metadata metadata = new Metadata();

OfficeParserConfig officeParserConfig = new OfficeParserConfig();
officeParserConfig.setIncludeHeadersAndFooters(false);
parseContext.set(OfficeParserConfig.class, officeParserConfig);

parser.parse(inputStream, contentHandler, metadata, parseContext);
System.out.println(contentHandler.toString());
Asad
  • 2,782
  • 2
  • 16
  • 17