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

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

相关推荐
yusheng_xyb4 分钟前
互联网大厂Java求职面试实录
java·面试·互联网·技术面试
百锦再4 分钟前
Spring Boot + JWT + RBAC 权限系统实战,从登录鉴权到接口级权限控制完整落地
java·数据库·spring boot·后端·sql·mysql·oracle
smxgn9 分钟前
FrankenPHP实践
java
小江的记录本12 分钟前
【Filter / Interceptor】过滤器(Filter)与拦截器(Interceptor)全方位对比解析(附底层原理 + 核心对比表)
java·前端·后端·spring·java-ee·前端框架·web
weisian15117 分钟前
Java并发编程--16-ConcurrentHashMap演进:从分段锁到CAS+synchronized
java·hashmap·分段锁·cas+同步·longaddr思想
福运常在18 分钟前
股票数据API(14)股票近年增发数据
java·python·maven
Java小王子呀23 分钟前
JAVA 导出Excel中添加下拉框用POI
java·excel
短剑重铸之日9 小时前
《ShardingSphere解读》07 读写分离:如何集成分库分表+数据库主从架构?
java·数据库·后端·架构·shardingsphere·分库分表
知我Deja_Vu9 小时前
【避坑指南】ConcurrentHashMap 并发计数优化实战
java·开发语言·python
daidaidaiyu10 小时前
Spring IOC 源码学习 事务相关的 BeanDefinition 解析过程 (XML)
java·spring