Arrangement of overlapping background PDF page stamps

Hello,

I’m replacing “behind text” images in PDF file with EPS images by using PDF stamps and I’m facing a problem of overlapping image arrangement. How could I change the order of overlapping images without replacing them backwards? I want the arrangement to be as in the before example:

Before:
image.jpg (221.0 KB)

After:
image.jpg (201.8 KB)

Here’s the used code example:

[...]

Document document = new Document(new FileInputStream("before stamp.pdf"));

Page page = document.getPages().get_Item(1);

ImagePlacementAbsorber pageImages = new ImagePlacementAbsorber();
page.accept(pageImages);

replaceImage(pageImages.getImagePlacements().get_Item(1), "tiger.eps");
replaceImage(pageImages.getImagePlacements().get_Item(2), "golfer.eps");

FileOutputStream pdfOutputStream = new FileOutputStream("after stamp.pdf");
document.save(pdfOutputStream);

[...]

void replaceImage(ImagePlacement imagePlacement, String filename) throws Exception {
    PsDocument vectorDocument = new PsDocument(new FileInputStream(filename));
    ByteArrayOutputStream vectorOutputStream = new ByteArrayOutputStream();
    vectorDocument.save(new PdfDevice(vectorOutputStream), new PdfSaveOptions());

    ByteArrayInputStream vectorInputStream = new 
    ByteArrayInputStream(vectorOutputStream.toByteArray());
    PdfPageStamp stamp = new PdfPageStamp(vectorInputStream, 1);
    Rectangle imageRectangle = imagePlacement.getRectangle();
    stamp.setHeight(imageRectangle.getHeight());
    stamp.setWidth(imageRectangle.getWidth());

    stamp.setXIndent(imageRectangle.getLLX());
    stamp.setYIndent(imageRectangle.getLLY());

    stamp.setBackground(true);

    imagePlacement.getPage().addStamp(stamp);
    imagePlacement.getImage().delete();
}

And here are the used documents:
Documents.zip (2.4 MB)

@ANDREA.FARRIS

We need to investigate this case to analyze the feasibility of the requirements. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43731

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.