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)