Java中word转pdf转png

  1. maven项目引包
 <!--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>
        <!--pdf转图片-->
        <dependency>
            <groupId>org.icepdf</groupId>
            <artifactId>icepdf-core</artifactId>
            <version>4.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.icepdf/icepdf-viewer -->
        <dependency>
            <groupId>org.icepdf</groupId>
            <artifactId>icepdf-viewer</artifactId>
            <version>4.1.1-taverna</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
        <dependency>
            <groupId>org.icepdf.os</groupId>
            <artifactId>icepdf-core</artifactId>
            <version>6.2.2</version>
        </dependency>

2.代码

 //PDF转图片(帮助文档)
    public List<String> getDataPDF(String imagePath) {
        File newFile = new File(manual);
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        List<String> strList = new ArrayList<>();
        try {
            //word转PDF   pdf文件名
            String pdfName = newFile.getName().substring(0, newFile.getName().lastIndexOf(".")) + ".pdf";
            File outputFile = new File(imagePath + pdfName);
            InputStream doc = new FileInputStream(newFile);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(doc).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            //PDF转图片
            List<String> imageList = PDFUtils.PDFImges(imagePath + pdfName, imagePath);
            for (String str : imageList
            ) {
                strList.add(httpPath + imagePath.replaceAll(imagePath.split("/")[0], "") + str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return strList;
    }

/**
     * PDF转图片
     *
     * @param
     * @return null
     * @Author HB
     * @Date 2021/4/13 09:56
     **/
    public static List<String> PDFImges(String pdfPath, String imagePath) {
        List<String> imageList = new ArrayList<>();
        Document document = new Document();
        try {
            document.setFile(pdfPath);
        } catch (Exception ex) {
        }
        //缩放比例
        float scale = 2.5f;
        //旋转角度
        float rotation = 0f;
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = null;
            try {
                image = (BufferedImage)
                        document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale);

                RenderedImage rendImage = image;
                try {
                    int a=i+100;
                    File file = new File(imagePath + "/help_" +a+ ".png");
                    ImageIO.write(rendImage, "png", file);
                    imageList.add(file.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                image.flush();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            document.dispose();
        }
        return imageList;
    }

3.友情提示
不能直接拿来就用,要稍作修改,即可使用!!!欢迎留言探讨。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值