java把pdf转换为word格式

java把pdf转换为word格式

第一步 导包
<dependency>
		<groupId>org.apache.pdfbox</groupId>
		<artifactId>pdfbox-tools</artifactId>
		<version>2.0.11</version>
	</dependency>
第二步 写代码块
 public static void convertText(String pdfPath){
        PDDocument doc = null;
        OutputStream fos = null;
        Writer writer = null;
        PDFTextStripper stripper = null;
        try {
            doc = PDDocument.load(new File(pdfPath));
            fos = new FileOutputStream(pdfPath.substring(0, pdfPath.indexOf(".")) + ".doc");
            writer = new OutputStreamWriter(fos, "UTF-8");
            stripper = new PDFTextStripper();
            int pageNumber = doc.getNumberOfPages();
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(pageNumber);
            stripper.writeText(doc, writer);
            writer.close();
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end..");
    }
 public static void main(String[] args) {

        convertText("D:\\BaiduNetdiskDownload\\javaee开发的颠覆者springboot实战.pdf");
    }

这样就简单又轻松的转变了pdf到word文件

发布了67 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/m0_37635053/article/details/104037311