org.apache.poi——将 office的各种类型文件(word等文件类型)转为 pdf

org.apache.poi------将 office的各种类型文件(word等文件类型)转为 pdf

简介

使用方法

word转pdf

Maven坐标为

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

核心方法如下所示。

bash 复制代码
//创建转换器
IConverter converter = LocalConverter.builder().build();
//转换需要的参数,依次是输入流、转换前的原类型、输出流、转换后的目标类型
boolean execute = converter.convert(docxInputStream)
                  .as(DocumentType.DOCX)
                  .to(outputStream)
                  .as(DocumentType.PDF)
                  .schedule().get();

使用示例

word转pdf

亲测有效

bash 复制代码
//    public static void main(String[] args) {
//        WordToPdfConverter converter = new WordToPdfConverter("C:\\Users\\LJH\\Desktop\\上传文件sha1为02.docx","C:\\Users\\LJH\\Desktop\\testConvert.pdf");
//        converter.convert();
//    }

public class WordToPdfConverter {

    String sourcePath;
    String targetPath;

    public WordToPdfConverter(String sourcePath, String targetPath){
        this.sourcePath = sourcePath;
        this.targetPath = targetPath;
    }


    /**
     * 输入:目前被限制为Path.toUri()
     * return:可能为null
     * */
    public File convert() {
        File inputWord = new File(sourcePath.toUri());
        File outputPdf = new File(targetPath.toUri());
        InputStream docxInputStream = null;
        OutputStream outputStream = null;
        try {
            docxInputStream = new FileInputStream(inputWord);
            outputStream = new FileOutputStream(outputPdf);
            IConverter converter = LocalConverter.builder().build();
            boolean execute = converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF)
                    .schedule().get();
            if (execute) {
                System.out.println("转换完毕 targetPath = " + outputPdf.getAbsolutePath());
            } else {
                System.out.println("[documents4J] word转pdf失败:");
                return null;
            }

            converter.shutDown();
        } catch (Exception e) {
            System.out.println("[documents4J] word转pdf失败:"+e.toString());
            return null;
        }finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (docxInputStream != null) {
                try {
                    docxInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return outputPdf;
    }
}
相关推荐
XLYcmy10 小时前
PDF 论文处理器 — 优势与功能文档
大数据·python·网络安全·pdf·dify·rag·漏洞检测
闭包不眠12 小时前
PDF文字提取交付前该检查什么
运维·服务器·图像处理·人工智能·计算机视觉·pdf
Latchh13 小时前
PDF提取到文字后怎样发现乱码
图像处理·人工智能·计算机视觉·pdf
XLYcmy15 小时前
PDF 论文处理器 — 技术报告文档
数据库·python·网络安全·pdf·embedding·dify·rag
deepseek2315 小时前
PDF 涂黑失效拆解:谷歌数据中心 52.65 兆瓦与 7.65 亿加仑的账本,被一次复制粘贴揭开
人工智能·pdf·数据中心
2601_949950631 天前
练题簿在线免费刷题 从资料整理到考前自测
面试·职场和发展·pdf·word·刷题
Dovis(誓平步青云)1 天前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
lsc_blog1 天前
远程和居家办公的经历,技术简历上怎么摆
面试·职场和发展·重构·pdf·求职招聘
文慧的科技江湖2 天前
2026年10月4日充电桩行业晚报:当“只充80%“成为通行规则,行业开始管单车占桩时长 | 慧知开源充电桩平台
开源·apache·新能源·虚拟电厂·充电桩·ocpp·v2g
鲲穹AI种草2 天前
Word 文档批量处理工具怎么选,多款工具实际使用情况整理
word·文档批量处理