We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to insert a textbox floating over a shape in word?

Hello
I insert a textbox floating over a shape, i set top=0, but the result actual has a difference.
my code:

String fileName = "/Downloads/image.docx";
String fileOutName = "/Downloads/image-out.docx";

Document document = new Document(fileName);

Shape shape = (Shape) document.getChild(NodeType.SHAPE, 0, true);

DocumentBuilder documentBuilder = new DocumentBuilder(document);
documentBuilder.moveTo(shape);

Shape textbox =
      documentBuilder.insertShape(ShapeType.TEXT_BOX, RelativeHorizontalPosition.CHARACTER, 0,
          RelativeVerticalPosition.PARAGRAPH, 0, 50, 50, WrapType.NONE);
textbox.setFillColor(new java.awt.Color(62, 59, 60));
textbox.setStroked(false);

Paragraph paragraph = textbox.getFirstParagraph();
Run run = new Run(document, "Test");
run.getFont().setColor(new java.awt.Color(231, 231, 231));
paragraph.appendChild(run);

document.save(fileOutName);

the result:

attachment:
image-out.docx (21.1 KB)
image.docx (26.8 KB)

Thanks.

@Rock_Zhang I think, in your case you can use LayoutCollector and LayoutEnumerator to get absolute position of shape and then use this position to insert absolutely positioned shape on top of the source shape:

Document document = new Document("C:\\Temp\\image.docx");
// Use LayoutCollector and LayoutEnumerator to get absolute position of shape.
LayoutCollector collector = new LayoutCollector(document);
LayoutEnumerator enumerator = new LayoutEnumerator(document);

Shape shape = (Shape) document.getChild(NodeType.SHAPE, 0, true);

enumerator.setCurrent(collector.getEntity(shape));

Shape textbox = new Shape(document, ShapeType.TEXT_BOX);
textbox.setWrapType(WrapType.NONE);
textbox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
textbox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

textbox.setWidth(50);
textbox.setHeight(50);
textbox.setTop(enumerator.getRectangle().getY());
textbox.setLeft(enumerator.getRectangle().getX());

textbox.appendChild(new Paragraph(document));

Paragraph paragraph = textbox.getFirstParagraph();
Run run = new Run(document, "Test");
run.getFont().setColor(new java.awt.Color(231, 231, 231));
paragraph.appendChild(run);

// Insert the created shape.
shape.getParentNode().insertAfter(textbox, shape);

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

out.docx (20.7 KB)

Hello

I need this effect: when I add a new paragraph in front of the shape, the text box on the shape can still follow the shape. This “out.docx” cannot do this effect, so is there any other way?

Thanks.

@Rock_Zhang You can use your approach, but shift your shape down. It looks like it is needed to be shifted to half of paragraph break font line spacing:

Shape textbox =
                documentBuilder.insertShape(ShapeType.TEXT_BOX, RelativeHorizontalPosition.CHARACTER, 0,
                        RelativeVerticalPosition.LINE, 0, 50, 50, WrapType.NONE);
textbox.setFillColor(new java.awt.Color(62, 59, 60));
textbox.setStroked(false);
textbox.setTop(shape.getParentParagraph().getParagraphBreakFont().getLineSpacing()/2);

Or as another option, you can calculate absolute rectangles of the main shape and inserted shape and then adjust vertical position of the inserted shape.

1 Like

Thanks, it’s working fine.

1 Like