PDF转成图片

1.使用pdfbox:

   代码如下:

public static void main(String[] args) {
        //第一种方式:
        String pdfPath = "d:\\深入理解.pdf";
        // config option 1:convert all document to image
        String[] args_1 = new String[3];
        args_1[0] = "-outputPrefix";
        args_1[1] = "my_image_1";
        args_1[2] = pdfPath;
        // 第二种方式
        String[] args_2 = new String[7];
        args_2[0] = "-startPage";
        args_2[1] = "1";
        args_2[2] = "-endPage";
        args_2[3] = "13";
        args_2[4] = "-outputPrefix";
        args_2[5] = "d:\\aa\\my_image";
        args_2[6] = pdfPath;
        try {
            // will output "my_image_1.jpg"
//          PDFToImage.main(args_1);
            // will output "my_image_2.jpg"
            PDFToImage.main(args_2);
        } catch (Exception e) {
//          logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
    }



2.使用pdf renderer:


public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File pdfFile = new File("d:\\深入理解.pdf");
            RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
            PDFFile pdf = new PDFFile(buf);
            for (int i=0; i<pdf.getNumPages(); i++)
                createImage(pdf.getPage(i), "d:\\bb\\PICTURE_"+i+".jpg");//输出路径,d盘下的bb文件中
        }
        public static void createImage(PDFPage page, String destination) throws IOException{
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),
                    (int) page.getBBox().getHeight());
            BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,
                             BufferedImage.TYPE_INT_RGB);
            Image image = page.getImage(rect.width, rect.height,    // width & height
                       rect,                       // clip rect
                       null,                       // null for the ImageObserver
                       true,                       // fill background with white
                       true                        // block until drawing is done
            );
            Graphics2D bufImageGraphics = bufferedImage.createGraphics();
            bufImageGraphics.drawImage(image, 0, 0, null);
            ImageIO.write(bufferedImage, "JPG", new File( destination ));
        }



jar包在网方下载

你可能感兴趣的:(TO,pdf,pdf,images,images,pdf转图片)