Replace table cell text with image

Hi
I am trying to replace text in a table cell with the image. I am not able to make it work, if I try to replace the text while iterating through the rows of the table. I am using aspose words for java version 20.7.
Can you please help me with this?
thanks

@lakshmi.vijayaraghavan You can use the following code to achieve this:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get the table.
Table table = doc.getFirstSection().getBody().getTables().get(0);

for (Row r : table.getRows())
{
    for (Cell c : r.getCells())
    {
        String cellText = c.toString(SaveFormat.TEXT).trim();
        if (cellText.equals("ImagePlaceholder"))
        {
            // Remove cell content.
            c.removeAllChildren();
            c.ensureMinimum();
            // Move DocumentBuilder into the cell and insert image.
            builder.moveTo(c.getFirstParagraph());
            builder.insertImage("C:\\Temp\\img.png");
        }
    }
}

doc.save("C:\\Temp\\out.docx");

Thank you very much, this worked.

1 Like