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

前言

java实现word文档转PDF,PDF转word

解决只能转换4页问题

解决每页头部存在水印问题

实现

引入依赖

xml 复制代码
<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 复制代码
package com.common.util;

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


import java.io.*;

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

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

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


}

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

Excel转PDF的实现方式可以参考:https://blog.csdn.net/m0_37969960/article/details/105519581

相关推荐
非优秀程序员1 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻2 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
文丑颜不良啊4 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang5 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
聪明的笨猪猪5 小时前
实际工程问题:AI 生成 Spring Cache(@Cacheable / @CachePut / @CacheEvict)缓存注解踩坑
java·经验分享·面试·ai编程
愿有所息6 小时前
记一次 ARM64 + JDK8 上 fastjson2 的诡异数据错乱
java
秋饼6 小时前
MCP 2026-07-28 无状态协议大改版:Spring AI 2.1 多轮往返请求与网关路由落地
java·ai·技术分享·后端开发
SL_staff7 小时前
JVS-BI 实战:业务人员如何用零代码 ELT + 可视化配置构建实时经营分析闭环
java·spring·全栈
SL_staff8 小时前
企业AI落地难?JVS-AI渐进式实践:安全可控、小切口验证、标准化沉淀与规模化复制
java·开源·aigc
SL_staff9 小时前
JVS数字底座实践:构建企业级统一内容管理平台的技术路径
java·产品