如何使用apache tika提取没有页眉和页脚的文档(pdf,docx,doc,odt)。
答案 0 :(得分:0)
我用所有文件格式测试了这段代码,有些解析得很好(pdf和html)而不适用于doc,docx,xlsx,xls格式
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());
}
}
}
答案 1 :(得分:0)
你可以用语法编写。以下是所有tika支持文档的工作方式,包括docx,pptx,odt pdf
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());