Example usage for org.apache.pdfbox.tools.imageio ImageIOUtil writeImage

List of usage examples for org.apache.pdfbox.tools.imageio ImageIOUtil writeImage

Introduction

In this page you can find the example usage for org.apache.pdfbox.tools.imageio ImageIOUtil writeImage.

Prototype

public static boolean writeImage(BufferedImage image, String formatName, OutputStream output, int dpi)
        throws IOException 

Source Link

Document

Writes a buffered image to a file using the given image format.

Usage

From source file:org.qifu.util.PdfConvertUtils.java

License:Apache License

public static List<File> toImageFiles(File pdfFile, int resolution) throws Exception {
    PDDocument document = PDDocument.load(pdfFile);
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    /*//w  ww. jav  a  2s.c o m
    List<PDPage> pages = new LinkedList<PDPage>();
    for (int i=0; i < document.getDocumentCatalog().getPages().getCount(); i++) {
       pages.add( document.getDocumentCatalog().getPages().get(i) );
    }
    */
    File tmpDir = new File(Constants.getWorkTmpDir() + "/" + PdfConvertUtils.class.getSimpleName() + "/"
            + System.currentTimeMillis() + "/");
    FileUtils.forceMkdir(tmpDir);
    List<File> files = new LinkedList<File>();
    //int len = String.valueOf(pages.size()+1).length();
    int len = String.valueOf(document.getDocumentCatalog().getPages().getCount() + 1).length();
    //for (int i=0; i<pages.size(); i++) {
    for (int i = 0; i < document.getDocumentCatalog().getPages().getCount(); i++) {
        String name = StringUtils.leftPad(String.valueOf(i + 1), len, "0");
        BufferedImage bufImage = pdfRenderer.renderImageWithDPI(i, resolution, ImageType.RGB);
        File imageFile = new File(tmpDir.getPath() + "/" + name + ".png");
        FileOutputStream fos = new FileOutputStream(imageFile);
        ImageIOUtil.writeImage(bufImage, "png", fos, resolution);
        fos.flush();
        fos.close();
        files.add(imageFile);
    }
    document.close();
    tmpDir = null;
    return files;
}