如何用java代码实现word转pdf

发布于:2024-06-28 ⋅ 阅读:(162) ⋅ 点赞:(0)

pom

  <!-- word -> pdf -->
        <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>

代码

   @Test
    public void pdf232() {
        long l = System.currentTimeMillis();
        String wordPath = "E:\\歌\\2022_江区_1716896947404\\日常\\2_取水户建月现场验.docx";
        String pdfPath = "E:\\歌\\2022_江区_1716896947404\\日常\\2_水户中每月现验.pdf";
        try {
            System.out.println(wordPath + "  开始转换.....");
            InputStream docxInputStream = new FileInputStream(wordPath);
            OutputStream outputStream = new FileOutputStream(pdfPath);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l);
            System.out.println(pdfPath + "  转换成功.....");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("导出成功......");
    }

效果

在这里插入图片描述

在这里插入图片描述

参考文章

https://blog.csdn.net/m0_59828167/article/details/139070897


网站公告

今日签到

点亮在社区的每一天
去签到