java tika pdf转图片_使用tika将pdf转为html
序
tika是個功能強大的項目,這里展示下如何使用tika來將pdf轉為html。
maven
org.apache.tika
tika-core
1.16
org.apache.tika
tika-parsers
1.16
轉換
public static String extractHtml(File file) throws IOException {
byte[] bytes = Files.toByteArray(file);
AutoDetectParser tikaParser = new AutoDetectParser();
ByteArrayOutputStream out = new ByteArrayOutputStream();
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler;
try {
handler = factory.newTransformerHandler();
} catch (TransformerConfigurationException ex) {
throw new IOException(ex);
}
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
handler.setResult(new StreamResult(out));
ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler);
try {
tikaParser.parse(new ByteArrayInputStream(bytes), handler1, new Metadata());
} catch (SAXException | TikaException ex) {
throw new IOException(ex);
}
return new String(out.toByteArray(), "UTF-8");
}
小結
效果對比的話,pdfDom的效果好一點。
總結
以上是生活随笔為你收集整理的java tika pdf转图片_使用tika将pdf转为html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java ajax解析json数据_利用
- 下一篇: java fileupload 文件_j