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 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
word·二次开发·office·com互操作
是Dream呀8 小时前
时序数据库选型指南:Apache IoTDB企业级解决方案深度解析
apache·时序数据库·iotdb
一个天蝎座 白勺 程序猿8 小时前
Apache IoTDB(5):深度解析时序数据库 IoTDB 在 AINode 模式单机和集群的部署与实践
数据库·apache·时序数据库·iotdb·ainode
阿幸软件杂货间20 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
智_永无止境1 天前
Apache Commons Math3 使用指南:强大的Java数学库
apache·math
reembarkation1 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation1 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
玩泥巴的1 天前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
word·二次开发·com互操作
Light601 天前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)
linux·spring boot·pdf·gpcl6/ghostpcl·s3/oss·权限与审计·异步与进度
一只 Lemon1 天前
jquery 文件上传 (CVE-2018-9207)漏洞复现
apache