用java代码实现pdf文档转word

今天是上班的第二个月,又是摸鱼的一天,一个同事说需要把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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值