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

相关推荐
森森-曦2 小时前
在word中如何设置从第二页开始页码编号
word
Secede.6 小时前
Windows + WSL2 + Docker + CudaToolkit:深度学习环境配置
windows·深度学习·docker
禾叙_7 小时前
【ollama】同一WiFi下Mac访问Windows Ollama失败?完整调试指南(从Connection refused到成功连通)
windows·macos
悟能不能悟8 小时前
list<string> 和String[],转化为jsonstr是不是一样的
数据结构·windows·list
请为小H留灯9 小时前
Windows 系统启用 Telnet 客户端:图文详细教程
网络·windows·测试·telnet
爱学习 爱分享10 小时前
word中批量替换
word
selina892111 小时前
word中脚注编号如何设置不显示?
word
信鸽爱好者11 小时前
Windows +VM虚拟机安装github服务器
服务器·windows·ubuntu·机器人·github
叫我莫言鸭12 小时前
关于word生成报告的POI学习
学习·word
分号13 小时前
你在运行该虚拟机时启用了侧通道缓解
windows