文件转换,简简单单,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>

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

相关推荐
海斗星河万里长4 小时前
ConvertAPI:PDF转Word的便捷之选
pdf
给我个面子中不4 小时前
JUC、JVM八股补充
java·开发语言·jvm
mask哥5 小时前
详解flink性能优化
java·大数据·微服务·性能优化·flink·kafka·stream
hqxstudying5 小时前
Kafka 深入研究:从架构革新到性能优化的全面解析
java·开发语言·微服务·kafka·springcloud
失散136 小时前
并发编程——17 CPU缓存架构详解&高性能内存队列Disruptor实战
java·缓存·架构·并发编程
only-qi11 小时前
146. LRU 缓存
java·算法·缓存
xuxie1312 小时前
SpringBoot文件下载(多文件以zip形式,单文件格式不变)
java·spring boot·后端
重生成为编程大王12 小时前
Java中的多态有什么用?
java·后端
666和77712 小时前
Struts2 工作总结
java·数据库
中草药z12 小时前
【Stream API】高效简化集合处理
java·前端·javascript·stream·parallelstream·并行流