aspose word轉換pdf檔案後將pdf檔案轉換為圖片png

oktokeep發表於2024-09-07

aspose word轉換pdf檔案後將pdf檔案轉換為圖片png


連結:aspose word模板檔案生成pdf
https://www.cnblogs.com/oktokeep/p/16615900.html

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文件轉換為圖片

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();
        }
    }
}

相關文章