Java实现Word文档转PDF,PDF转Word,PDF转Excel,PDF转换工具

news/2024/7/21 4:27:57 标签: java, word, pdf, excel, springboot

前言

java实现word文档转PDF,PDF转word
解决只能转换4页问题
解决每页头部存在水印问题

实现

引入依赖

<dependency>
   <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.0.3</version>
</dependency>

破解的jar包
链接: https://pan.baidu.com/s/1MO8OBuf4FQ937R9KDtofPQ 提取码: 4tsn

java">word">package com.common.util;

word">import com.aspose.pdf.Document;
word">import com.aspose.pdf.SaveFormat;
word">import com.documents4j.api.DocumentType;
word">import com.documents4j.api.IConverter;
word">import com.documents4j.job.LocalConverter;


word">import java.io.*;

/**
 * PDF转换工具类
 * @author yyq
 */
word">public word">class PdfUtil {

    word">public word">static word">void main(String[] args) {

        //pdfToObj("C:\\Users\\Administrator\\Desktop\\测试.pdf", "docx");

        String filePath = "C:\\Users\\Administrator\\Desktop\\测试.docx";
        String outFilePath = "C:\\Users\\Administrator\\Desktop\\测试.pdf";
        objToPdf(filePath, outFilePath, "docx");
    }

    /**
     * PDF 转 doc、Excel、xml
     * @param pdfPath 需要转换的pdf路径
     * @param suffix 文件后缀
     */
    word">public word">static word">void pdfToObj(String pdfPath, String suffix) {
        word">long old = System.currentTimeMillis();
        FileOutputStream os = word">null;
        word">try {
            // 新建一个word文档
            String wordPath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + "." + suffix;
            os = word">new FileOutputStream(wordPath);
            // doc是将要被转化的word文档
            Document doc = word">new Document(pdfPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            word">if(suffix.equals("doc")){
                doc.save(os, SaveFormat.Doc);
            }word">else word">if(suffix.equals("docx")){
                doc.save(os, SaveFormat.DocX);
            }word">else word">if(suffix.equals("xls") || suffix.equals("xlsx")){
                doc.save(os, SaveFormat.Excel);
            }word">else word">if(suffix.equals("html")){
                doc.save(os, SaveFormat.Html);
            }word">else word">if(suffix.equals("xml")){
                doc.save(os, SaveFormat.Xml);
            }
            os.close();
            // 转化耗时
            word">long now = System.currentTimeMillis();
            word">long useTime = ((now - old) / 1000);
            System.out.println("Pdf 转 Word 共耗时:" + useTime + "秒");
        } word">catch (Exception e) {
            System.out.println("Pdf 转 Word 失败...");
            e.printStackTrace();
        }word">finally {
            word">try {
                os.close();
            } word">catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * txt、doc、Excel、xml 转 PDF
     * @param oidPath 需要转的文件路径
     * @param newPath 转成的PDF文件路径
     */
    word">public word">static word">void objToPdf(String oidPath, String newPath, String suffix){
        InputStream inputStream = word">null;
        OutputStream outputStream = word">null;
        IConverter converter = word">null;
        word">try {
            // 源文件地址
            File oidFile = word">new File(oidPath);
            // 导出文件地址
            File newFile = word">new File(newPath);
            // 文件读取
            inputStream = word">new FileInputStream(oidFile);
            outputStream = word">new FileOutputStream(newFile);
            // 开始转换
            converter = LocalConverter.builder().build();
            word">boolean flag = false;
            word">if(suffix.equals("doc")){
                flag = converter.convert(inputStream).as(DocumentType.DOC).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("docx")){
                flag = converter.convert(inputStream).as(DocumentType.DOCX).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("txt")){
                flag = converter.convert(inputStream).as(DocumentType.TEXT).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("xls")){
                flag = converter.convert(inputStream).as(DocumentType.XLS).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("xlsx")){
                flag = converter.convert(inputStream).as(DocumentType.XLSX).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("html")){
                flag = converter.convert(inputStream).as(DocumentType.MHTML).word">to(outputStream).as(DocumentType.PDF).execute();
            }word">else word">if(suffix.equals("xml")){
                flag = converter.convert(inputStream).as(DocumentType.XML).word">to(outputStream).as(DocumentType.PDF).execute();
            }
            word">if (flag) {
                converter.shutDown();
            }
            inputStream.close();
            outputStream.close();
            System.out.println("转换成功");
        } word">catch (Exception e) {
            converter.shutDown();
            e.printStackTrace();
            System.out.println("转换失败");
        }word">finally {
            word">try {
                inputStream.close();
                outputStream.close();
            } word">catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

源码路径:https://download.csdn.net/download/weixin_43992507/88215577


http://www.niftyadmin.cn/n/4938600.html

相关文章

elementUI时间选择器el-time-picker的坑

//开始时间<el-time-pickerplaceholder"选择时间":format"HH:mm:ss" //显示的时间样式value-format"HH:mm:ss" //绑定值的样式 //不给默认为 Date 对象值&#xff1a;"2023-07-31T16:00:00.000Z"v-model"FormData.startTime&…

研发工程师玩转Kubernetes——启动、存活和就绪探针

启动&#xff08;Startup Probe&#xff09;、存活&#xff08;Liveness Probe&#xff09;和就绪探针(Readiness Probe)有其不同的用途和优先级。 优先级和用途 启动探针&#xff08;Startup Probe&#xff09;用于Pod内程序告诉kubernetes&#xff0c;其准备工作已经做好。…

Docker容器与虚拟化技术:Docker架构、镜像管理

目录 一、理论 1.Doker概述 2.Docker核心概念 3.Docker安装 4.Docker的镜像管理命令 二、实验 1.Docker安装 2.查看Docker信息 3.Docker的镜像管理命令 三、问题 1.如何注册Docker账号 2.如何设置Docker公共存储库 四、总结 一、理论 1.Doker概述 (1) IT架构 裸…

Airbnb开源数据可视化工具Visx

一、什么是visx visx 是用于 React 的富有表现力的底层可视化组件集合,结合了 d3 的强大功能来生成可视化,以及 React 更新 DOM 的诸多优势。 在 Airbnb 内部,visx 的目标是统一整个公司的可视化堆栈,在此过程中,创建了 visx 项目,从而有效的将 D3 的强大功能与 React …

深入浅出 栈和队列(附加循环队列、双端队列)

栈和队列 一、栈 概念与特性二、Stack 集合类及模拟实现1、Java集合中的 Stack2、Stack 模拟实现 三、栈、虚拟机栈、栈帧有什么区别&#xff1f;四、队列 概念与特性五、Queue集合类及模拟实现1、Queue的底层结构&#xff08;1&#xff09;顺序结构&#xff08;2&#xff09;链…

rabbitmq的持久化

目录 队列实现持久化 如何删除队列​编辑 消息实现持久化 不公平分发 如何保障当 RabbitMQ 服务停掉以后消息生产者发送过来的消息不丢失。默认情况下 RabbitMQ 退出或由于某种原因崩溃时&#xff0c;它忽视队列和消息&#xff0c;除非告知它不要这样做。确保消息不会丢失需…

汽车电子的发展对国产32位MCU的影响

32位MCU是目前产品市场的主流&#xff0c;尤其是在汽车电子发展起来后&#xff0c;32位MCU在市场的比重迅速增涨&#xff0c;国内32位MCU厂商抓住风口&#xff0c;推出一系列汽车电子芯片从而稳定国内市场。如芯海的CS32F030系列就是主要应用汽车电子市场的32位MCU。 根据市场…

8,四个类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast

类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast const_castreinterpret_castdynamic_caststatic_cast const_cast 被const修饰的函数可以被访问&#xff0c;但是不能被修改成员变量 const_cast可以去掉const #include <iostream> using namespace s…