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

相关推荐
耿直小伙42 分钟前
电脑已连接网络无线自动重启
windows
m0_565611131 小时前
Java-泛型
java·windows
蓁蓁啊2 小时前
Ubuntu 虚拟机文件传输到 Windows的一种好玩的办法
linux·运维·windows·单片机·ubuntu
serve the people3 小时前
Comma-Separated List Output Parser in LangChain
windows·langchain·list
E_ICEBLUE4 小时前
Python 教程:如何快速在 PDF 中添加水印(文字、图片)
开发语言·python·pdf
缺点内向6 小时前
Java: 为PDF批量添加图片水印实用指南
java·开发语言·pdf
2501_930707787 小时前
使用C#代码在 Word 文档中查找并替换文本
开发语言·c#·word
百***58148 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
造价女工10 小时前
PDF转图片:轻松实现工程图纸的高效共享与高清展示
pdf·格式工厂
IDOlaoluo10 小时前
Windows系统调校_20250408_36367c06.exe安装教程(附详细步骤)
windows