admin 管理员组

文章数量: 1184232

目录

前言

一、简易版本(只可以将PDF转为文字)

二、完整版本

1.引入库

2.编写工具类

3.编写controller类

4.vue前端实现下载

5.提示

三、注意事项

总结


前言

在日常工作学习中经常会遇到需要将PDF文件转成Word文件的场景,但是现在网上绝大部分PDF转Word的软件都是打着免费的噱头吸引你下载,但是使用时才发现,这些软件只能免费转几页,剩下的还是要购买会员。但是实际要转的PDF页数都远远超过了免费额度,这个时候,我们就可以通过这篇文章,自己写一个工具接口,来达到我们PDF转Word的目的

一、简易版本(只可以将PDF转为文字)

1、首先,进入Apache官网下载pdfbox最新版jar包,官网地址为:https://pdfbox.apache/download.cgi

2、将jar包导入到项目中,具体步骤如下:

(1)在项目resources目录下新建lib文件夹。并将下载的pdfbox最新版jar包放入lib文件夹

 

 

 

(2)进入project structure中导入项目中jar包

 

(3)在Modules目录下点击右方+添加jar包

 

(4)选择JARs and Directories

 

(5)选择jar包所在目录并点击ok

3、编写代码进行文件转换,代码如下:

public static void main(String[] args) {

    try {

        String pdfFile = "E:/乙方链账号.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 

本文标签: 项目 pdf SpringBoot maven Word