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

Free Support Forum - aspose.com

Set paragraph space after/before in textbox using Java

Hello. I need to add a text box with internal margins = 0 to the existing document (textbox-margin.docx) on archive. I need to add it to the first paragraph on the document. But this paragraph has a preset margins(11, 11).
For some reason my shape inherits these values and I cannot set other values manually.
Please help how I can do it?

private void insertShape() throws Exception {
        Document document = new Document("textbox-margin.docx");

        Shape strShape = new Shape(document, ShapeType.TEXT_BOX);
        strShape.setTop(10);
        strShape.setLeft(10);

        strShape.setWidth(40);
        strShape.setHeight(40);

        strShape.getTextBox().setInternalMarginTop(0);
        strShape.getTextBox().setInternalMarginBottom(0);
        strShape.getTextBox().setInternalMarginLeft(0);
        strShape.getTextBox().setInternalMarginRight(0);

        strShape.appendChild(new Paragraph(document));
        Paragraph para = strShape.getFirstParagraph();
        para.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
        Run run = new Run(document);
        run.setText("text");
        para.appendChild(run);

        document.getFirstSection().getBody().getFirstParagraph().appendChild(strShape);

        document.save(UUID.randomUUID() + ".docx", SaveFormat.DOCX);
    }

textbox-margin.zip (45.4 KB)

@AWD

We have tested the scenario using the latest version of Aspose.Words for Java 20.1 and have not found the shared issue. So, please use Aspose.Words for Java 20.1.

I also have tested on 20.1 and has the same problem. 11pt/11pt instead 0.
image.png (30.9 KB)

Result file:
7a5ae72f-b28c-453e-966e-1e0d2b8333b6.zip (52.5 KB)

Do you have any ideas?

Sorry guys, my mistake.
I had to use

documentBuilder.getParagraphFormat().setSpaceBefore(0);
documentBuilder.getParagraphFormat().setSpaceAfter(0);

for that!
Thanks for the help!

@AWD

It is nice to hear from you that your problem has been solved. We suggest you please read the following article. Hope this helps you.
Using DocumentBuilder to Modify a Document