码迷,mamicode.com
首页 > 编程语言 > 详细

java PDF2JPG

时间:2018-08-09 19:26:10      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:for   mod   document   common   i++   buffered   cep   red   apach   

import org.apache.commons.lang3.StringUtils;
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;

    private static void pdfTojpg(String pdfFilePath, String jpgFilePath) {
        File pdfFile = new File(pdfFilePath);
        int idx = jpgFilePath.lastIndexOf('.');
        String jpgprefix = StringUtils.substring(jpgFilePath, 0, idx);
        int pdfdpi = 400;
        try (final PDDocument document = PDDocument.load(pdfFile, "")) {
            int size = document.getNumberOfPages();
            for (int i = 0; i < size; i++) {
                BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);
                File jpgFile = new File(jpgprefix + "_" + i + ".jpg");
                ImageIO.write(image, "jpg", jpgFile);
            }
        } catch (Exception e) {
        }

    }

java PDF2JPG

标签:for   mod   document   common   i++   buffered   cep   red   apach   

原文地址:https://www.cnblogs.com/lanqie/p/9450904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!