admin管理员组文章数量:1605134
今天是上班的第二个月,又是摸鱼的一天,一个同事说需要把pdf文档转换成word文档,他说软件转需要花钱,我说用open office免费的,然后他就把文件发给我,我打开open office后发现我好像不会太会用,然后想了一下,作为一个程序员,这种东西用代码来实现不过分吧!首先肯定是去百度啦,果然百度出来了!接下来上过程和代码:
首先随便找个pom文件把核心的jar包干进去 然后记得maven一下
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.21</version>
</dependency>
然后就是写个main方法
public static void main(String[] args) { try { String pdfFile = "C:\\Users\\ThinkPad\\Desktop\\申报材料申请书-废水回用水扩充项目.pdf"; PDDocument doc = PDDocument.load(new File(pdfFile)); int pagenumber = doc.getNumberOfPages(); pdfFile = pdfFile.substring(0, pdfFile.lastIndexOf(".")); String fileName = pdfFile + ".doc"; File file = new File(fileName); if (!file.exists()){ file.createNewFile(); } FileOutputStream fos = new FileOutputStream(fileName); Writer writer = new OutputStreamWriter(fos, "UTF-8"); PDFTextStripper stripper = new PDFTextStripper(); stripper.setSortByPosition(true);// 排序 stripper.setStartPage(1);// 设置转换的开始页 stripper.setEndPage(pagenumber);// 设置转换的结束页 stripper.writeText(doc, writer); writer.close(); doc.close(); System.out.println("pdf转换word成功!"); } catch (IOException e) { e.printStackTrace(); } }
最后run就完事儿了
最后桌面出现了这玩意儿 申报材料申请书-废水回用水扩充项目.doc
版权声明:本文标题:用java代码实现pdf文档转word 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728480093a1160015.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论