pdf 转 图片

pdf 转图片

借鉴了别人的实现,但是,原文没有贴出import代码,导致使用的人,无法很清晰的知道需要导入的包。


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Cheng Jinquan on 2017/6/9.
 */
public class Pdf2Img {


    public static void main(String[] args) {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Pdf2Img.pdf2Img_icepdf("E:\\test\\pdf\\test.pdf", "E:\\test\\pdf\\icepdf");
                    }
                }
        ).start();

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Pdf2Img.pdf2Img_pdfbox("E:\\test\\pdf\\test.pdf", "E:\\test\\pdf\\pdfbox");
                    }
                }
        ).start();

        try {
            Thread.sleep(120000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void pdf2Img_icepdf(String pdfPath, String imgPath) {
        System.out.println("icepdf start...");
        long time = System.currentTimeMillis();
        Document document = new Document();
        try {
            document.setFile(pdfPath);
        } catch (PDFException e) {
            e.printStackTrace();
        } catch (PDFSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        float scale = 2.5f;//缩放比例
        float rotation = 0f;//旋转角度
        long timeTmp = System.currentTimeMillis();
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                File file = new File(imgPath+"\\iecPDF_" + i + ".png");
                ImageIO.write(rendImage, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
            long time2 = System.currentTimeMillis();
            System.out.println("icepdf "+(time2-timeTmp));
            timeTmp = time2;
        }
        document.dispose();
        System.out.println("icepdf over..."+(System.currentTimeMillis()-time));
    }

    /**
     * rederImageWithDPI的第二个参数为dpi分辨率单位,可根据需求调节大小,代码第八行提供了架包里另一种转图片的方法,第二个参数为缩放比。
     * @param pdfPath
     * @param imgPath
     */
    public static void pdf2Img_pdfbox(String pdfPath, String imgPath) {
        System.out.println("icepdf start...");
        long time = System.currentTimeMillis();
        File file = new File(pdfPath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            long timeTmp = System.currentTimeMillis();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                //BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, "PNG", new File(imgPath+"\\pdfbox_image"+i+".png"));
                long time2 = System.currentTimeMillis();
                System.out.println("icepdf " + (time2 - timeTmp));
                timeTmp = time2;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("icepdf over..."+(System.currentTimeMillis()-time));
    }

    public void pdf2Img_jpedal() {
        /*PdfDecoder decode_pdf = new PdfDecoder(true);
        try {
            decode_pdf.openPdfFile("c:\\test.pdf"); //file
//       decode_pdf.openPdfFile("C:/jpedalPDF.pdf", "password"); //encrypted file
//      decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF
//      decode_pdf.openPdfFileFromURL("http://www.mysite.com/jpedalPDF.pdf",false);
//      decode_pdf.openPdfFileFromInputStream(in, false);

            int start = 1, end = decode_pdf.getPageCount();
            for (int i = start; i < end + 1; i++) {
                BufferedImage img = decode_pdf.getPageAsImage(i);
                try {
                    ImageIO.write(img, "png", new File("C:\\jpedal_image.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            decode_pdf.closePdfFile();
        } catch (PdfException e) {
            e.printStackTrace();
        }*/
    }
}



<!-- icepdf start -->
<dependency>
	<groupId>org.sejda</groupId>
	<artifactId>sejda-icepdf</artifactId>
	<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.icepdf.os</groupId>
	<artifactId>icepdf-core</artifactId>
	<version>6.1.2</version>
</dependency>
<dependency>
	<groupId>org.icepdf.os</groupId>
	<artifactId>icepdf-viewer</artifactId>
	<version>6.1.2</version>
</dependency>
<!-- icepdf end -->
<!-- pdfbox start -->
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>fontbox</artifactId>
	<version>2.0.3</version>
</dependency>
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.3</version>
</dependency>
<!-- pdfbox end -->
<!-- jpedal pdf start -->
<dependency>
	<groupId>org.jpedal</groupId>
	<artifactId>OpenViewerFX</artifactId>
	<version>7.2.30</version>
</dependency>
<!-- jpedal pdf end -->        


借鉴链接   http://www.cnblogs.com/pcheng/p/5704470.html



import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Cheng Jinquan on 2017/6/9.
 */
public class Pdf2Img {


    public static void main(String[] args) {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Pdf2Img.pdf2Img_icepdf("E:\\test\\pdf\\test.pdf", "E:\\test\\pdf\\icepdf");
                    }
                }
        ).start();

        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        Pdf2Img.pdf2Img_pdfbox("E:\\test\\pdf\\test.pdf", "E:\\test\\pdf\\pdfbox");
                    }
                }
        ).start();

        try {
            Thread.sleep(120000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void pdf2Img_icepdf(String pdfPath, String imgPath) {
        System.out.println("icepdf start...");
        long time = System.currentTimeMillis();
        Document document = new Document();
        try {
            document.setFile(pdfPath);
        } catch (PDFException e) {
            e.printStackTrace();
        } catch (PDFSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        float scale = 2.5f;//缩放比例
        float rotation = 0f;//旋转角度
        long timeTmp = System.currentTimeMillis();
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
            RenderedImage rendImage = image;
            try {
                File file = new File(imgPath+"\\iecPDF_" + i + ".jpg");
                ImageIO.write(rendImage, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            image.flush();
            long time2 = System.currentTimeMillis();
            System.out.println("icepdf "+(time2-timeTmp));
            timeTmp = time2;
        }
        document.dispose();
        System.out.println("icepdf over..."+(System.currentTimeMillis()-time));
    }

    /**
     * rederImageWithDPI的第二个参数为dpi分辨率单位,可根据需求调节大小,代码第八行提供了架包里另一种转图片的方法,第二个参数为缩放比。
     * @param pdfPath
     * @param imgPath
     */
    public static void pdf2Img_pdfbox(String pdfPath, String imgPath) {
        System.out.println("icepdf start...");
        long time = System.currentTimeMillis();
        File file = new File(pdfPath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            long timeTmp = System.currentTimeMillis();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 144);
                //BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, "PNG", new File(imgPath+"\\pdfbox_image"+i+".png"));
                long time2 = System.currentTimeMillis();
                System.out.println("icepdf " + (time2 - timeTmp));
                timeTmp = time2;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("icepdf over..."+(System.currentTimeMillis()-time));
    }

    public void pdf2Img_jpedal() {
        /*PdfDecoder decode_pdf = new PdfDecoder(true);
        try {
            decode_pdf.openPdfFile("c:\\test.pdf"); //file
//       decode_pdf.openPdfFile("C:/jpedalPDF.pdf", "password"); //encrypted file
//      decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF
//      decode_pdf.openPdfFileFromURL("http://www.mysite.com/jpedalPDF.pdf",false);
//      decode_pdf.openPdfFileFromInputStream(in, false);

            int start = 1, end = decode_pdf.getPageCount();
            for (int i = start; i < end + 1; i++) {
                BufferedImage img = decode_pdf.getPageAsImage(i);
                try {
                    ImageIO.write(img, "png", new File("C:\\jpedal_image.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            decode_pdf.closePdfFile();
        } catch (PdfException e) {
            e.printStackTrace();
        }*/
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值