java PDF转JPG

第一步:
引入jar:

<!-- PDF转jpg  -->
<dependency>
     <groupId>org.apache.pdfbox</groupId>
     <artifactId>fontbox</artifactId>
     <version>2.0.1</version>
 </dependency>
 <dependency>
     <groupId>org.apache.pdfbox</groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.1</version>
 </dependency>
<!-- PDF转jpg  -->

第二步:
java代码:

package cn.com.XX.utils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
 
/**
 * 
 * @author zj
 *
 */
public class Pdf2jpgUtils {
 
    public static void main(String args[]){
        pdfbox();
    }
 
 
    static void pdfbox()  {
        PDDocument doc = null;
        ByteArrayOutputStream os = null;
        InputStream stream = null;
 
        OutputStream out = null;
 
        /*InputStream is = null;
        OutputStream responseOut = null;*/
        try{
            long start = System.currentTimeMillis();
            //pdf路径
            stream = new FileInputStream(new File("D:/pdf.pdf"));
            // 加载解析PDF文件
            doc = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            PDPageTree pages = doc.getPages();
            int pageCount = pages.getCount();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
                os = new ByteArrayOutputStream();
                ImageIO.write(bim, "jpg", os);
                byte[] datas = os.toByteArray();
 
                //jpg文件转出路径
                out = new FileOutputStream("D:\\pdf\\abc" + i + ".jpg");
                out.write(datas);
 
                //通过response输出流
               /* is = new ByteArrayInputStream(datas);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) > 0) {
                    responseOut.write(buffer, 0, len);
                }
                responseOut.flush();*/
 
            }
            long end = System.currentTimeMillis();
            long time = (end - start);
            System.out.println("pdf转jpg耗时: " + time);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (doc != null) {
                try {
                    doc.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            /*if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (responseOut != null) {
                try {
                    responseOut.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
        }
 
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值