word、excel文件转PDF(documents4j方式,简单)

1 documents4j方式

引入pom

java 复制代码
<dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.1.12</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.1.12</version>
        </dependency>

#excel转pdf需要的jar
<dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-excel</artifactId>
            <version>1.1.12</version>
        </dependency>

2 代码实现(word,excel转pdf代码)

java 复制代码
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public static void main(String[] args) {
        File inputWord = new File("E:\\111\\123.docx");
        File outputFile = new File("E:\\111\\123.pdf");
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
java 复制代码
/**
     * documents4j方式excel转pdf
     * @param file 源文件
     * @return 返回转成功的文件
     */
    public static File wordToPDFByDocuments4j(File file) {
        File outputFile = new File(String.format("%s%s%s%s", file.getParentFile(),  File.separator, System.currentTimeMillis(), ".pdf"));
        try  {
            InputStream docxInputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputFile;
    }

2 poi方式

相关推荐
x***r1517 小时前
node-v0.12.2-x86安装步骤详解(附Node.js历史版本与32位配置教程)
windows
小小小米粒7 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
蜡台8 小时前
Python包管理工具pip完全指南-----2
linux·windows·python
love530love13 小时前
Windows Podman Machine 虚拟硬盘迁移完整指南:从 C 盘到非系统盘
c语言·人工智能·windows·podman
love530love13 小时前
Podman Machine 虚拟硬盘迁移实战二:用 Junction 把 vhdx 从 C 盘搬到其他盘
c语言·开发语言·人工智能·windows·wsl·podman·podman machine
薛定猫AI15 小时前
【技术干货】Claude Code 终端编程实战:从零搭建 Windows 高效 AI 开发环境
人工智能·windows
jiangbqing15 小时前
职场动物进化手册(升级版).pdf 免费分享
pdf·职场动物净化·职场必读潜规则
合合技术团队16 小时前
智能合同审查搭建教程:低质量PDF怎么处理?先解析清洗,再分路审阅(附GitHub项目地址)
pdf·prompt·github·textin
V我五十买鸡腿18 小时前
网安基础 Windows 和 Linux 那些常用命令
linux·运维·windows
dbkx_2919 小时前
Word域操作记录(从2开始的公式编号排版)
word