ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java PDF转JPG

2020-12-02 10:58:44  阅读:257  来源: 互联网

标签:Exception java JPG try new catch PDF null pdfbox


第一步:
引入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();
                }
            }*/
        }
 
    }
}

标签:Exception,java,JPG,try,new,catch,PDF,null,pdfbox
来源: https://blog.csdn.net/joy_tom/article/details/110471721

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有