gpt4 book ai didi

java - PDFBox 未检测到页面中的图像

转载 作者:行者123 更新时间:2023-11-30 08:10:08 26 4
gpt4 key购买 nike

我正在尝试检测 this 中的图像使用 PDFBox 生成 pdf 文件。该 pdf 文件有两张空白图像,一张位于左侧(“将其放入盒子中”文本下方),另一张位于右侧(“将其粘贴在盒子外”文本下方)。这是我用来检测图像的代码:

PDPage page = (PDPage) catalog.getAllPages().get(0);
PDStream contents = page.getContents();
PDFStreamParser parser = new PDFStreamParser(contents.getStream());
parser.parse();
List<Object> tokens = parser.getTokens();

PDResources resources = page.getResources();
Map<String, PDXObjectImage> images = resources.getImages();
if(null != images){
Iterator<String> it = images.keySet().iterator();
while(it.hasNext()){
String key = it.next();
System.out.println("Key >>>>>>>>>>>>>> "+key);
}
}

我能够检测到第二张图像。但是,未检测到第一张图像。问题是什么?我确信 pdf 是正确的。我创建了多次,但仍然面临同样的问题。我使用 Sketch 创建了 pdf。

谢谢。

最佳答案

简而言之

I'm able to detect the second image. However, the first image is not being detected. What is the problem?

实际上,两个页面图像使用相同的图像资源,只是拉伸(stretch)到不同的尺寸。

详细

如果您查看页面的内容流,您会在末尾看到以下内容:

q
720 0 0 970 832 126 cm
/Im1 Do
Q
q
512 0 0 128 144 968 cm
/Im1 Do
Q

前四行在位置 832 绘制图像资源 Im1,126 拉伸(stretch)到 720 x 970,最后 4 行在位置绘制相同的图像资源 Im1 144、968 拉伸(stretch)至 512 x 128。

做什么

您仅仅查看页面资源来查找页面图像的方法是不合适的,因为

  • 正如您所见,单个图像资源可以在拉伸(stretch)到不同尺寸的页面上多次使用,
  • 页面上可能根本不使用图像资源(例如,某些文档具有从所有页面引用的一个大资源字典;对于给定页面,可能不会使用许多资源),
  • 图像可以内联到内容流中;你的方法根本看不到这些图像,并且
  • 表单 Xobjects 或 Patterns 可能会显示在您的页面上,它们可能分别在自己的资源中包含图像;由于您只查看直接页面资源中包含的图像资源,因此您的方法也找不到它们。

PDFBox 示例 PrintImageLocations 中提供了更好的解决方案(仅对内联图像和可能有图案的图像失败)。您的文件的输出是

*******************************************************************
Found image [Im1]
position = 832.0, 128.0
size = 360px, 462px
size = 720.0, 970.0
size = 10.0in, 13.472222in
size = 254.0mm, 342.19446mm

*******************************************************************
Found image [Im1]
position = 144.0, 128.0
size = 360px, 462px
size = 512.0, 128.0
size = 7.111111in, 1.7777778in
size = 180.62222mm, 45.155556mm

此示例使用 PDFBox PDFStreamEngine解析处理后的内容以绘制页面。

关于java - PDFBox 未检测到页面中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30499409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com