I am using pdfbox 2 to get all the images from a page.
I am using the code below to extract the images. It works fine.
What I need to determine is the top most image when there are multiple images on top of each other.
Using acrobat the command “send to front” or “send to back” can be used. So I assume there is some info that tells me the layer or depth or something ?? It could be I just do not know the term to search for. thanks for any help
public class ImgPositionEngine extends PDFGraphicsStreamEngine {
int pageNum;
int cnt=0;
public ImgPositionEngine(PDPage page, int pageNum) {
super(page);
this.pageNum = pageNum;
}
@Override
public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException {
System.out.printf("appendRectangle %.2f %.2f, %.2f %.2f, %.2f %.2f, %.2f %.2fn",
p0.getX(), p0.getY(), p1.getX(), p1.getY(),
p2.getX(), p2.getY(), p3.getX(), p3.getY());
}
@Override
public void fillAndStrokePath(int windingRule) throws IOException {
//System.out.println("fillAndStrokePath");
}
@Override
public void shadingFill(COSName shadingName) throws IOException {
System.out.println("shadingFill " + shadingName.toString());
}
@Override
public void drawImage(PDImage pdImage) throws IOException {
if (pdImage instanceof PDImageXObject) {
PDImageXObject image = (PDImageXObject) pdImage;
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
Point2D.Float position = new Point2D.Float(ctm.getTranslateX(), ctm.getTranslateY());
boolean isPlaceHolderImage = false;
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
Matrix ctmNew = getGraphicsState().getCurrentTransformationMatrix();
float imageXScale = ctmNew.getScalingFactorX();
float imageYScale = ctmNew.getScalingFactorY();
String fname = "f_"+pageNum+"_"+cnt+".png";
File file = new File("/out/" + fname );
ImageIO.write((image).getImage(), "png", file);
cnt++;
}
}
public ArrayList<RectInfo> getRectInfos() {
return rectInfos;
}
@Override
public void strokePath() throws IOException {
// No operation
}
@Override
public void fillPath(int windingRule) throws IOException {
// No operation
}
@Override
public void clip(int windingRule) throws IOException {
// No operation
}
@Override
public void moveTo(float x, float y) throws IOException {
// No operation
}
@Override
public void lineTo(float x, float y) throws IOException {
// No operation
}
@Override
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException {
// No operation
}
@Override
public Point2D getCurrentPoint() throws IOException {
return null;
}
@Override
public void closePath() throws IOException {
// No operation
}
@Override
public void endPath() throws IOException {
// No operation
}
}