文件转换,简简单单,pdf转word,不要去找收费的了,自己学了之后免费转,之后就复制粘贴就ok了

先上一个链接pdf转word文件转换

接口层

java 复制代码
    @PostMapping("pdfToWord")

    public String  pdfToWord(@RequestParam("file") MultipartFile file) throws IOException {

        String fileName = FileExchangeUtil.pdfToWord(file.getInputStream(),file.getName());
        return fileName;

    }

方法层-----一个方法直接搞定

java 复制代码
  /**
     * 只是单纯的文字转换,没有任何的格式
     *
     * @param inputStream 文件流
     * @return
     */
    public static String pdfToWord(InputStream inputStream, String fileName) {
        //创建一个堆系pdf对象
        PDDocument document = null;
        FileOutputStream outputStream = null;
        if (Objects.isNull(fileName)) {
            fileName = FileExchangeUtil.getRandomString();
        }
        try {
            document = PDDocument.load(inputStream);
            PDFTextStripper stripper = new PDFTextStripper();
            //获取文本内容
            String text = stripper.getText(document);
            //创建word文档
            XWPFDocument doc = new XWPFDocument();
            XWPFParagraph p = doc.createParagraph();
            XWPFRun r = p.createRun();
            r.setText(text);
            //保存word
            outputStream = new FileOutputStream(new File("./file/"+fileName + ".docx"));
            doc.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                outputStream.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
                return null;
            }
            return null;
        }
        return fileName;

    }

需要的依赖

java 复制代码
 <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.40</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

经得起实操,不要怪我没有整理最终生成的格式,实在有些东西不好搞,只能放放了

相关推荐
无声旅者1 小时前
深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
java·ide·ai·intellij-idea·ai编程·continue·openapi
敲代码的小吉米1 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
Ryan-Joee1 小时前
Spring Boot三层架构设计模式
java·spring boot
Hygge-star1 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
dkmilk2 小时前
Tomcat发布websocket
java·websocket·tomcat
工一木子2 小时前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis
哞哞不熬夜2 小时前
JavaEE--初识网络
java·网络·java-ee
等等5432 小时前
Java EE初阶——wait 和 notify
java·开发语言
API小爬虫3 小时前
淘宝按图搜索商品(拍立淘)Java 爬虫实战指南
java·爬虫·图搜索算法
lyrhhhhhhhh3 小时前
Spring 框架 JDBC 模板技术详解
java·数据库·spring