How to replace text by an image and not overlap existing text

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 :frowning: :
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

@qmeritte

Thank you for contacting support.

We are afraid that the text contents may not re-position or pushed downwards while making space for the image. Text will exist on the PDF page as it is. Moreover, Aspose.PDF for Java API mimics the behavior of Adobe Acrobat. If you are able to relocate the contents while adding an image to existing PDF file then please share respective files with us along with the steps so that we may investigate further to help you out.