我一直在尝试使用pdfbox从pdf中提取图像,将它们转换为缓冲图像并保存。
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。我需要缓冲图像,因为我需要将它与另一个图像进行比较。怎么解决这个?