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

相关推荐
qq_24218863321 小时前
快速搭建跨环境检测服务的步骤
linux·开发语言·windows·python·macos
呉師傅1 小时前
【操作技巧】Win10关机提示这个应用阻止关机。怎么解决?
运维·网络·windows·电脑
FL16238631291 小时前
windows从源码安装python版本paddleocr3.4.0
开发语言·windows·python
halen3333 小时前
Transform PDFs into Stunning Images with Free PDF to IMG!
pdf
开开心心_Every3 小时前
局域网大文件传输,设密码双向共享易用工具
运维·服务器·网络·游戏·pdf·电脑·excel
x***r1514 小时前
TUPortable安装步骤详解(附流量监控与进程占用查看教程)
windows
2501_907136824 小时前
图片转word文档
word·软件需求
开开心心就好6 小时前
实用PDF批量加马赛克,抹除敏感信息绿色版
java·linux·开发语言·网络·人工智能·pdf·word2vec
Zzz 小生6 小时前
LangChain Messages:消息使用完全指南
数据库·windows·microsoft
发现你走远了16 小时前
Windows 下手动安装java JDK 21 并配置环境变量(详细记录)
java·开发语言·windows