admin管理员组文章数量:1130349
先添加依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.28</version>
</dependency>
private byte[] pdf2word(String filePath, byte[] in) throws IOException {
String ext = FileUtil.getExt(filePath);
if ("pdf".equals(ext)) {
//将pdf加载到对象中去
PDDocument doc = PDDocument.load(in);
//得到pdf的页数
int pagenumber = doc.getNumberOfPages();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);//排序
stripper.setStartPage(1);//设置转换的开始页
stripper.setEndPage(pagenumber);//设置转换的结束页
stripper.writeText(doc, writer);
writer.close();
return baos.toByteArray();
}
return null;
}
会丢失部分格式,但是整体还行,毕竟免费的要啥自行车
先添加依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.28</version>
</dependency>
private byte[] pdf2word(String filePath, byte[] in) throws IOException {
String ext = FileUtil.getExt(filePath);
if ("pdf".equals(ext)) {
//将pdf加载到对象中去
PDDocument doc = PDDocument.load(in);
//得到pdf的页数
int pagenumber = doc.getNumberOfPages();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(baos, StandardCharsets.UTF_8);
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);//排序
stripper.setStartPage(1);//设置转换的开始页
stripper.setEndPage(pagenumber);//设置转换的结束页
stripper.writeText(doc, writer);
writer.close();
return baos.toByteArray();
}
return null;
}
会丢失部分格式,但是整体还行,毕竟免费的要啥自行车
版权声明:本文标题:pdfbox 将pdf转换为word 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1763669408a2952992.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论