Shape with type ShapeType.TEXT_BOX set a specific width

Hi, please help.
I need to insert a shape on the first page into existing document(DOCX). Then I need to set a specific width for shape. But the shape in any case cuts the size to the size of the entered text.

I’m using version: aspose.words 19.8.jar

Screenshot_8.jpg (26.6 KB)

private Shape insertStrShape(String str, DocumentBuilder documentBuilder,
double topPosition, double leftPos, double initWidth, int textAlignment) throws Exception {
documentBuilder.moveToDocumentStart();
documentBuilder.moveTo(documentBuilder.getCurrentSection().getBody().getFirstParagraph());
Shape strShape = documentBuilder.insertShape(ShapeType.TEXT_BOX,
RelativeHorizontalPosition.PAGE, leftPos,
RelativeVerticalPosition.PAGE, topPosition,
initWidth,
9.637795,
WrapType.NONE);

strShape.setWidth(initWidth);
strShape.setZOrder(Constants.SHAPE_Z_ORDER);
// strShape.setStroked(false);
strShape.setFilled(false);
strShape.getTextBox().setInternalMarginTop(0);
strShape.getTextBox().setInternalMarginBottom(0);
strShape.getTextBox().setInternalMarginLeft(0);
strShape.getTextBox().setInternalMarginRight(0);
strShape.getTextBox().setTextBoxWrapMode(TextBoxWrapMode.NONE);
strShape.getTextBox().setFitShapeToText(false);

documentBuilder.moveTo(strShape.getFirstParagraph());
documentBuilder.write(str);

//default line spacing 1.0: ParagraphFormatting LineSpacingRule property default value - #4 by tahir.manzoor
documentBuilder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
documentBuilder.getParagraphFormat().setLineSpacing(12);

documentBuilder.getParagraphFormat().setAlignment(textAlignment);

Run run = strShape.getFirstParagraph().getRuns().get(0);

run.getFont().clearFormatting();
run.getFont().setName(“Times New Roman”);
run.getFont().setSize(8);
run.getFont().setColor(Color.BLACK);

return strShape;
}

@AWD,

Instead of 0, you can try specifying 72 value inside the setInternalMarginLeft property. The following code produces expected output: awjava-19.10.zip (5.4 KB)

Document doc  = new Document();
DocumentBuilder  documentBuilder  = new DocumentBuilder(doc);

documentBuilder.moveToDocumentStart();
documentBuilder.moveTo(documentBuilder.getCurrentSection().getBody().getFirstParagraph());
Shape strShape = documentBuilder.insertShape(ShapeType.TEXT_BOX,
        RelativeHorizontalPosition.PAGE, 288,
        RelativeVerticalPosition.PAGE, 288,
        144,
        9.637795,
        WrapType.NONE);

strShape.setWidth(200);
//strShape.setZOrder(Constants.SHAPE_Z_ORDER);
// strShape.setStroked(false);
strShape.setFilled(false);
strShape.getTextBox().setInternalMarginTop(0);
strShape.getTextBox().setInternalMarginBottom(0);
strShape.getTextBox().setInternalMarginLeft(72);
strShape.getTextBox().setInternalMarginRight(0);
strShape.getTextBox().setTextBoxWrapMode(TextBoxWrapMode.NONE);
strShape.getTextBox().setFitShapeToText(false);

documentBuilder.moveTo(strShape.getFirstParagraph());
documentBuilder.write("HELLO");

//default line spacing 1.0: ParagraphFormatting LineSpacingRule property default value
documentBuilder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
documentBuilder.getParagraphFormat().setLineSpacing(12);

documentBuilder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

Run run = strShape.getFirstParagraph().getRuns().get(0);

run.getFont().clearFormatting();
run.getFont().setName("Times New Roman");
run.getFont().setSize(8);
run.getFont().setColor(Color.BLACK);

doc.save("E:\\Temp\\awjava-19.10.docx");

Thank you, it really works! But I don’t understand why. Where can I read about this magic constant 72?

UPD: I understand, but I think that this is not what I need.

If the length of the text in 2 different shapes is different then they will differ in the width of the shape, I need the shapes to have the same specified width and not depend on the length of the entered text.

Text alignment in a shape can be right/left.

I also need to set a static position for this shapes.

Screenshot_111.jpg (22.9 KB)

@AWD,

You can obtain the desired width/height by using the following simple code:

Document doc = new Document();

Shape anotherShape = new Shape(doc, ShapeType.TEXT_BOX);
anotherShape.setWrapType(WrapType.INLINE);
anotherShape.setWidth(72 * 4); // 4 inches
anotherShape.setHeight(72 * 1); // 1 inches

Paragraph para = new Paragraph(doc);
Run runText = new Run(doc, "This is inline Text Box");
para.appendChild(runText);

anotherShape.appendChild(para);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(anotherShape);

doc.save("E:\\Temp\\awjava-19.10.docx");

Thanks, size problem occurs if I use strShape.getTextBox().setTextBoxWrapMode(TextBoxWrapMode.NONE)
now I just don’t use it

@AWD,

Thanks for the details. In case you have further inquiries or need any help, please let us know.