Insert Text into Shape in Word Document using Java | Add Paragraph Text Content inside Text Box Shape

Hello!
How can I set text into the shape?
I go into the shape with using builder.moveTo(shape) and write the text using builder.writeln(‘text’) but text write above this shape. Shape’s lastParagraph is null.

@ramazanovam910,

Please add a Paragraph inside Shape, move cursor to that Paragraph and then write text. Here is sample Java code:

Document document = new Document("E:\\Temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(document);

Shape shape = new Shape(document, ShapeType.TEXT_BOX);
shape.setWidth(72 * 2);
shape.setHeight(72 * 2);

Paragraph para = new Paragraph(document);
shape.appendChild(para);

builder.insertNode(shape);
builder.moveTo(para);

builder.write("Inside Shape");

document.save("E:\\temp\\awjava-20.5.docx");
1 Like

@awais.hafeez

That’s work!
Thank you!