我有一段代码可以从 PDF 中收集图像并使用 pdfBox 将它们保存在文件夹中。这些图像是无用的,因为我对它们一无所知。PDF 包含章节标题,后跟 1-3 张图片。无论如何要更改程序,以便它会告诉我他们来自哪个部分?
这是代码:
public static void main(String[] args) throws IOException {
PDDocument document = null;
try {
document = PDDocument.load("C:\\Users\\564864\\Downloads\\wsh2012.pdf");
} catch (IOException ex) {
System.out.println("" + ex);
}
List pages = document.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
int i =1;
String name = null;
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
PDResources resources = page.getResources();
Map pageImages = resources.getImages();
if (pageImages != null) {
Iterator imageIter = pageImages.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
image.write2file("C:\\Users\\564864\\Desktop\\Java\\helloworld\\images\\" + i+"");
i ++;
}
}
}
}