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;
    }
}
相关推荐
琪伦的工具库2 小时前
批量PDF文件加密工具使用说明:固定密码或随机密码批量加密PDF,权限控制+导出密码记录
运维·服务器·pdf
云计算老刘2 小时前
Keepalived + LVS(DR)+ Apache + NFS
apache·lvs
其实秋天的枫2 小时前
【26专四】英语专业四级TEM4历年真题及答案解析电子版PDF(2009-2025年)
经验分享·pdf
优化控制仿真模型2 小时前
26年初中中考英语大纲词汇1600个电子版PDF
经验分享·pdf
优化控制仿真模型2 小时前
【26专四】英语专业四级TEM4历年真题及答案电子版PDF(2009-2025年)
经验分享·pdf
羑悻的小杀马特2 小时前
工业时序数据库选型:从数据模型与存储引擎看 Apache IoTDB
apache·时序数据库·iotdb
Jermy Li2 小时前
HugeGraph 正式晋升 Apache 顶级项目:重塑「图 + AI」底座
数据库·人工智能·apache·知识图谱·database·hugegraph·knowledge graph
一位代码2 小时前
python | 使用 pdfplumber 库提取 pdf 中的所有超链接
pdf
可涵不会debug2 小时前
时序数据库选型深度指南:Apache IoTDB——大数据时代的优选方案
apache·时序数据库·iotdb
切糕师学AI3 小时前
Apache Solr 详解:企业级搜索平台的核心特性与架构
架构·apache·solr