java pdf转jpg

package com.xxx;

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.*;

public class pdf2jpg {

    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:\\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();
                }
            }*/
        }

    }
}
        
            org.apache.pdfbox
            fontbox
            2.0.1
        
        
            org.apache.pdfbox
            pdfbox
            2.0.1
        

分为两种不同的输出,既可以直接输出到本地文件,也可以输出到流之中,只不过输出流最好是将多张图片的流合成为一张图片然后输出流。

你可能感兴趣的:(工具类,java,pdf,pdf转jpg,jpg,代码)