当前位置: 首页 > 工具软件 > PDFBox > 使用案例 >

pdfbox-Pdf转图片

南宫才艺
2023-12-01
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.12</version>
		</dependency>

 


package com.example.exampleservice.util;

import com.aspose.pdf.exceptions.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
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;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PdfUtil {

    public static void pdfToJpg(String pdfFilePath, String imagePath) throws InvalidPasswordException, IOException {
        PDDocument document = PDDocument.load(new File(pdfFilePath));
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        List<BufferedImage> images = new ArrayList<>();
        int pageCounter = 0;
        int i = 0;
        for (PDPage page : document.getPages()) {
            i++;
            // note that the page number parameter is zero based
            BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter++, 300, ImageType.RGB);
            images.add(bim);
            ImageIO.write(bim, "jpg", new File(imagePath + i + ".jpg"));
        }

    }

}

 

 类似资料: