aspose word转换pdf文件后将pdf文件转换为图片png

2024-10-09 12:33:24 浏览数 (3)

aspose word转换pdf文件后将pdf文件转换为图片png

需要的jar

aspose-cells-8.5.2.jar

aspose-pdf-15.8.0.jar

aspose-word-15.8.0.jar

aspose-words-19.5jdk.jar

链接:aspose word模板文件生成pdf

https://cloud.tencent.com/developer/article/2455660

代码语言:javascript复制
package com.example.core.mydemo.aspose;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class PdfToImage {
    public static void main(String[] args) {
        String path = "D:\pdf\40255371604299_1_sign.pdf";
        String path2 = "D:\pdf";
        pdfFileToImages(path,path2);

    }

    public static void pdfFileToImages(String filePath, String cachePath) {
        try {
            PDDocument pd = PDDocument.load(new File(filePath));
            PDFRenderer pdfRenderer = new PDFRenderer(pd);
            /* dpi越大转换后越清晰,相对转换速度越慢 */
            int pages = pd.getNumberOfPages();
            System.out.println("pdf总共多少页-----"   pages);
            for (int page = 0; page < pages;   page) {
                BufferedImage image = pdfRenderer.renderImageWithDPI(page, 96, ImageType.RGB);
                //将图片数据保存为PNG格式文档
                File file= new File(cachePath   File.separator   ("new" page)   ".png");
                ImageIO.write(image, "png", file);
                System.out.println(file.getAbsolutePath());
                System.out.println("PDF文档转PNG图片成功!");
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }

}

另外一种方法不适用,将word中的图片转换为图片,而不是将word文档转换为图片

代码语言:javascript复制
package com.example.core.mydemo.aspose;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * 将word中的图片转换为图片,而不是将word文档转换为图片
 * https://blog.51cto.com/u_16213439/8905749
 */
public class WordToImageConverter {
    public static void main(String[] args) {
        try {
            String wordFilePath = "D:\pdf\testword.docx";
            String outputImagePath = "D:\pdf\image33.png";

            XWPFDocument document = new XWPFDocument(new FileInputStream(wordFilePath));

            List<XWPFPictureData> pictures = document.getAllPictures();
            System.out.println("pictures size="   pictures.size());

            for (int i = 0; i < pictures.size(); i  ) {
                XWPFPictureData picture = pictures.get(i);
                byte[] pictureData = picture.getData();
                BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictureData));

                String imageFileName = outputImagePath   "_"   (i   1)   ".png";
                if(image != null) {
                    ImageIO.write(image, "png", new FileOutputStream(new File(imageFileName)));
                }
            }

            document.close();
            System.out.println("image ok 2");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0 人点赞