java pdf 转word

使用java api 简单实现pdf文档转word文档功能。
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import java.io.*;

public class PdfToWord {

    public static void main(String[] args) {
        String pdfPath =  "D:\\Tools\\Spring.pdf";

        try {
            PDDocument doc = PDDocument.load(new File(pdfPath));
            int pageNum = doc.getNumberOfPages();
            pdfPath = pdfPath.substring(0, pdfPath.lastIndexOf("."));
            String fileName = pdfPath+".doc";
            File file = new File(fileName);
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            Writer writer = new OutputStreamWriter(fos,"UTF-8");
            PDFTextStripper textStripper = new PDFTextStripper();
            textStripper.setSortByPosition(true);
            textStripper.setStartPage(1);
            textStripper.setEndPage(pageNum);
            textStripper.writeText(doc,writer);
            writer.close();
            doc.close();

            System.out.println("转换成功");

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

本文所用的jar包,pdfbox-1.8.2.jar,jdk 1.8 ,由于使用gradle构建 ,compile("org.apache.pdfbox:pdfbox:1.8.2"),也可以自行下载jar包。

本来有个pdf文档90多页,但是有没有标签,阅读麻烦,作为一个程序员,果断使用程序实现,结果转换过来的文件没有格式,没有达到预期效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值