当前位置: 首页 > news >正文

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

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

接口层


    @PostMapping("pdfToWord")

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

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

    }

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

  /**
     * 只是单纯的文字转换,没有任何的格式
     *
     * @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;

    }

需要的依赖

 <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>

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

相关文章:

  • 关于缓存和数据库一致性问题的深入研究
  • 2023.11.19 hadoop之MapReduce
  • 网络层——IP协议
  • 【C++】【Opencv】霍夫直线检测即cv::HoughLinesP()函数详解和示例
  • 云计算(Docker)
  • 此芯科技加入绿色计算产业联盟,参编绿色计算产业发展白皮书
  • Python大数据之linux学习总结——day10_hive调优
  • 系列六、GC垃圾回收【四大垃圾算法-标记清除算法】
  • git使用及常用命令
  • 【Go入门】并发
  • 【Proteus仿真】【51单片机】锂电池管理系统
  • 《QT从基础到进阶·三十三》QT插件开发QtPlugin
  • vue3 实现pdf预览
  • JAXB实现XML和Bean相互转换
  • css 设置网页最小字体为12px
  • Flutter 中在单个屏幕上实现多个列表
  • Go vs Rust:文件上传性能比较
  • 循环神经网络RNN完全解析:从基础理论到PyTorch实战
  • 【ArcGIS Pro二次开发】(76):面积平差工具
  • [C/C++]数据结构 LeetCode:用栈实现队列
  • 昆明警方重拳打击经济领域违法犯罪:去年抓获905名嫌犯
  • 安徽省委副秘书长、省委政研室主任余三元调任省社科院院长
  • 独行侠以1.8%概率获得状元签,NBA原来真的有剧本?
  • 香港根据《维护国家安全条例》订立附属法例
  • 威尼斯建筑双年展总策划:山的另一边有什么在等着我们
  • 2025上海科技节本周六启幕,机器人和科学家同走AI科学红毯