Hello Team,
I would like to fill the shape inside all the section named “fill_my_box” with certain colour and some random text in center. PFA
Lorem Ipsum.docx (26.2 KB)
Any idea how this could be achieved. TIA
Hello Team,
I would like to fill the shape inside all the section named “fill_my_box” with certain colour and some random text in center. PFA
Lorem Ipsum.docx (26.2 KB)
Any idea how this could be achieved. TIA
@nileshmishra To make it easier to identify shapes in your document you can set Shape.Name or Shape.AlternativeText properties. Shape.AlternativeText is easier to use because there is no easy way to set shape name in MS Word. Then in the code you can use the condition to identify the shape:
Document doc = new Document("C:\\Temp\\in.docx");
for (Shape s : (Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true))
{
if (s.getAlternativeText().equals("fill_my_box"))
{
// Change background color.
s.setFillColor(Color.YELLOW);
// Remove old content of the shapes.
s.removeAllChildren();
s.getTextBox().setVerticalAnchor(TextBoxAnchor.MIDDLE);
// Add content.
Paragraph p = new Paragraph(doc);
p.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
p.appendChild(new Run(doc, "This is some text"));
s.appendChild(p);
}
}
doc.save("C:\\Temp\\out.docx");
Thanks for suggestions, will check this one.