如何从pdf中提取图像

时间:2015-07-03 11:16:03

标签: java android image pdf bitmap

我正在尝试使用JPDFImages,Jpedal,PDFRenderer等从android中的pdf文件中提取图像,但我无法提取图像。这是我尝试使用PDFRenderer jar的代码,

RandomAccessFile raf;
        try {
            raf = new RandomAccessFile(file, "r");

            FileChannel channel = raf.getChannel();
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
            PDFFile pdffile = new PDFFile(buf);
            // draw the first page to an image
            int num=pdffile.getNumPages();
            for(int i=0;i<num;i++)
            {
                com.sun.pdfview.PDFPage page = pdffile.getPage(i);

                //get the width and height for the doc at the default zoom              
                int width=(int)page.getBBox().getWidth();
                int height=(int)page.getBBox().getHeight();             

                Rectangle rect = new Rectangle(0,0,200,200);
                int rotation=page.getRotation();
                Rectangle rect1=rect;
                if(rotation==90 || rotation==270)
                    rect1=new Rectangle(0,0,rect.height,rect.width);

                //generate the image
                BufferedImage img = (BufferedImage)page.getImage(
                            rect.width, rect.height, //width & height
                            rect1, // clip rect
                            null, // null for the ImageObserver
                            true, // fill background with white
                            true  // block until drawing is done
                    );

                ImageIO.write(img, "png", new File(args[1]+i+".png"));
            }
        } 
        catch (FileNotFoundException e1) {
            System.err.println(e1.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println(e.getLocalizedMessage());
        }

任何人都可以帮助我摆脱这个问题。 提前谢谢。

0 个答案:

没有答案
相关问题