Hi,
I am trying to replace some text by an image in my pdf document, and while I managed to do this i noticed the text around the image was hidden by the image. Is there a way to have the image push the text so that it doesn’t cover it ?
Here is my original text :
inPdf.pdf (152.0 KB)
Here is what I want :
exemple.pdf (147.5 KB)
And here is what i get instead :
outPdf.pdf (208.4 KB)
Here is the function I’m using :
private static void replaceTags(String inPdf, String outPdf, String tag, String imageName) throws FileNotFoundException, IOException {
// Open document
Document pdfDocument = new Document(inPdf);
for (Page page : pdfDocument.getPages()) {
java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(imageName));
// Add an image to the Images collection of the page resources
page.getResources().getImages().add(imageStream);
// Using the GSave operator: this operator saves current graphics state
page.getContents().add(new Operator.GSave());
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(tag);
page.accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (TextFragment textFragment : textFragmentCollection) {
Rectangle rectangle = textFragment.getRectangle();
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
Matrix matrix = new Matrix(new double[] { ximage.getWidth(), 0, 0, ximage.getHeight(), rectangle.getLLX(), rectangle.getLLY() });
page.getContents().add(new Operator.ConcatenateMatrix(matrix));
// Using Do operator: this operator draws image
page.getContents().add(new Operator.Do(ximage.getName()));
// Using GRestore operator: this operator restores graphics state
page.getContents().add(new Operator.GRestore());
//Remove textFragment
textFragment.setText("");
}
imageStream.close();
}
// Save the updated PDF file
pdfDocument.save(outPdf);
}
Thanks