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

相关推荐
啥咕啦呛11 小时前
java打卡学习1:Lambda表达式与Stream基础
windows
SEO-狼术12 小时前
Improve Navigation with In-Cell Hyperlinks
python·pdf
态态态12 小时前
平板PDF充足笔记空间的最优解
笔记·pdf
优选资源分享13 小时前
zTasker v2.3.8 便携版丨 Windows 自动化任务工具
运维·windows·自动化
reasonsummer14 小时前
【办公类-142-03】20260304插班生word转长表EXCLE(3)从word表格按行导出列表,提取索引内容。写入EXCLE长表,另存有名字的文件名
word
CQU_JIAKE14 小时前
3.23[Q]s
开发语言·windows·python
WJX_KOI15 小时前
OpenClaw 纯 Windows 环境源码部署教程(含修改脚本配置)
人工智能·windows
ulias21216 小时前
函数栈帧的创建和销毁
开发语言·数据结构·c++·windows·算法
攻城狮在此17 小时前
Windows电脑如何关闭不必要启动项,提升开机速度与运行流畅度
windows
ljh57464911917 小时前
linux xargs 命令
linux·运维·windows