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;
    }
}
相关推荐
shouchaobao5 小时前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
熊文豪6 小时前
工业物联网时序数据库选型指南:Apache IoTDB 的技术优势与应用实践
物联网·apache·时序数据库·iotdb
一个天蝎座 白勺 程序猿8 小时前
Apache IoTDB(10):数据库操作——从查询到优化的全链路实践指南
数据库·apache·时序数据库·iotdb
云计算老刘8 小时前
1.项目实战:LAMP-LNMP-分离部署
linux·mysql·php·apache·mariadb
SelectDB10 小时前
字节跳动:Apache Doris + AI 一站式融合数据引擎的探索与实践
数据库·apache
南风微微吹10 小时前
2026年最新国考《行测》《申论》历年真题及答案PDF电子版(2000-2025年)
pdf·国考
可涵不会debug13 小时前
时序数据库选型指南:大数据时代下Apache IoTDB的崛起之路
大数据·apache·时序数据库
p***q7813 小时前
【保姆级教程】apache-tomcat的安装配置教程
java·tomcat·apache
SelectDB14 小时前
Apache Doris 中的 Data Trait:性能提速 2 倍的秘密武器
数据库·后端·apache
q***017718 小时前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf