找到你要的答案

Q:Extracting images from pdf using java

Q:从PDF使用java提取图像

I have been trying to extract images from a pdf using pdfbox, converting them into buffered image and saving them.

 document = PDDocument.load("path");
        List pages = document.getDocumentCatalog().getAllPages();
        Iterator iter = pages.iterator();
        while( iter.hasNext() )
        {
            PDPage page = (PDPage)iter.next();
            PDResources resources = page.getResources();
            Map images = resources.getImages();
            if( images != null )
            {
                Iterator imageIter = images.keySet().iterator();
                int i = 0;
                while( imageIter.hasNext() )
                {
                    i++;
                    String key = (String)imageIter.next();
                    PDXObjectImage image = (PDXObjectImage)images.get( key );
                    BufferedImage b = image.getRGBImage();
                    File outputfile = new File("D:\\Image\\A"+i+".png");
                    ImageIO.write(b, "png", outputfile);
                }
            }
        }

but while converting into buffered image getRGBImage function is returning null. i need buffered image because i need to compare it with another image. how to solve this?

我一直试图从PDF文件使用pdfbox提取图像,转换成缓冲图像并保存。

 document = PDDocument.load("path");
        List pages = document.getDocumentCatalog().getAllPages();
        Iterator iter = pages.iterator();
        while( iter.hasNext() )
        {
            PDPage page = (PDPage)iter.next();
            PDResources resources = page.getResources();
            Map images = resources.getImages();
            if( images != null )
            {
                Iterator imageIter = images.keySet().iterator();
                int i = 0;
                while( imageIter.hasNext() )
                {
                    i++;
                    String key = (String)imageIter.next();
                    PDXObjectImage image = (PDXObjectImage)images.get( key );
                    BufferedImage b = image.getRGBImage();
                    File outputfile = new File("D:\\Image\\A"+i+".png");
                    ImageIO.write(b, "png", outputfile);
                }
            }
        }

但是,当转换到缓冲图像getrgbimage函数返回null。我需要缓冲的图像,因为我需要比较它与另一个图像。如何解决这个问题?

java  pdf  pdfbox