add color/gray detection

This commit is contained in:
Jörg Prante 2023-11-24 23:20:48 +01:00
parent 2fdccf2c79
commit 319dd41658

View file

@ -8,6 +8,8 @@ import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources; import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.graphics.image.PDImage; import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
@ -99,10 +101,9 @@ public class DocumentAnalyzer {
m.put("mediabox", Map.of("height", page.getMediaBox().getHeight(), "width", page.getMediaBox().getWidth())); m.put("mediabox", Map.of("height", page.getMediaBox().getHeight(), "width", page.getMediaBox().getWidth()));
m.put("bleedbox", Map.of("height", page.getBleedBox().getHeight(), "width", page.getBleedBox().getWidth())); m.put("bleedbox", Map.of("height", page.getBleedBox().getHeight(), "width", page.getBleedBox().getWidth()));
m.put("rotation", page.getRotation()); m.put("rotation", page.getRotation());
List<Map<String, Object>> list = new ArrayList<>(); ImageGraphicsExtractor extractor = new ImageGraphicsExtractor(page);
ImageGraphicsExtractor extractor = new ImageGraphicsExtractor(list, page);
extractor.process(); extractor.process();
m.put("images", list); m.put("images", extractor.getList());
List<Map<String, Object>> fonts = new ArrayList<>(); List<Map<String, Object>> fonts = new ArrayList<>();
PDResources res = page.getResources(); PDResources res = page.getResources();
for (COSName cosName : res.getFontNames()) { for (COSName cosName : res.getFontNames()) {
@ -125,18 +126,21 @@ public class DocumentAnalyzer {
private final List<Map<String, Object>> list; private final List<Map<String, Object>> list;
protected ImageGraphicsExtractor(List<Map<String, Object>> list, PDPage page) { protected ImageGraphicsExtractor(PDPage page) {
super(page); super(page);
this.list = list; this.list = new ArrayList<>();
} }
public void process() throws IOException { public void process() throws IOException {
processPage(getPage()); processPage(getPage());
} }
public List<Map<String, Object>> getList() {
return list;
}
@Override @Override
public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) { public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) {
} }
@Override @Override
@ -152,6 +156,8 @@ public class DocumentAnalyzer {
m.put("height", xobject.getHeight()); m.put("height", xobject.getHeight());
m.put("bitspercomponent", xobject.getBitsPerComponent()); m.put("bitspercomponent", xobject.getBitsPerComponent());
m.put("colorspace", xobject.getColorSpace().getName()); m.put("colorspace", xobject.getColorSpace().getName());
m.put("iscolor", PDDeviceRGB.INSTANCE.getName().equals(xobject.getColorSpace().getName()));
m.put("isgray", PDDeviceGray.INSTANCE.getName().equals(xobject.getColorSpace().getName()));
m.put("suffix", xobject.getSuffix()); m.put("suffix", xobject.getSuffix());
list.add(m); list.add(m);
} }