How to set vertical alignment of TextBox using Java

I would like to have vertically aligned text inside a textBox.

Java Code:

import com.aspose.words.*;

import java.io.File;

public class AsposeBugExample {

    public static void main(String[] args) {
        try {
            com.aspose.words.Document doc = new Document();
            doc.removeAllChildren();
            Section section = new Section(doc);
            doc.appendChild(section);
            DocumentBuilder documentBuilder = new DocumentBuilder(doc);

            final Shape node = documentBuilder.insertShape(ShapeType.TEXT_BOX, 350, 200);
            node.setHorizontalAlignment(HorizontalAlignment.LEFT);
            node.setVerticalAlignment(VerticalAlignment.CENTER);
            node.setTop(150);
            node.setLeft(450);
            node.getTextBox().setVerticalAnchor(TextBoxAnchor.MIDDLE);
            final Paragraph paragraph = node.getLastParagraph();
            final Run row = new Run(documentBuilder.getDocument(),"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            paragraph.appendChild(row);

            doc.save(System.getProperty("user.dir") + File.separator + "textbox_vertical_middle.docx");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Result and expected files:
vertical_aligment.zip (28.4 KB)

Reproduce expected file:

  1. Blank document → Insert → Shapes → Text Box → Add text “aaaaaaaa” into text box
  2. Shape Format → Align Text → Middle

Current state:
Text in text box is not aligned vertically.

Using versions:
Java : 11
aspose-words : 20.8

Code modeled on responses in:

@aolo23

Please use the following OoxmlSaveOptions to get the desired output. Hope this helps you.

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
doc.save(MyDir + "textbox_vertical_middle.docx", options);
1 Like

It works correctly :slight_smile:

@tahir.manzoor

Update:
Unfortunately, it does not work properly with photos. I will try to prepare examples.
diffrent_results.zip (38.1 KB)

In file without_OoxmlSaveOptions.docx I have 2 images
In file with_OoxmlSaveOptions.docx they are minimalised. (it is an unwanted result)

Java Code presents the difference :

import com.aspose.words.Shape;
import com.aspose.words.*;

import java.awt.*;
import java.io.File;

public class AsposeBugExample {

    public static void main(String[] args) {
        verticalAlignmentBug();
    }

    public static void verticalAlignmentBug() {
        try {
            // Create empty docx
            com.aspose.words.Document doc = new Document();
            doc.removeAllChildren();
            Section section = new Section(doc);
            doc.appendChild(section);
            DocumentBuilder documentBuilder = new DocumentBuilder(doc);


            //Page setup
            PageSetup ps = documentBuilder.getPageSetup();
            ps.setPageWidth(595.275634765625);
            ps.setPageHeight(841.8897705078125);
            ps.setOrientation(Orientation.PORTRAIT);
            ps.setTopMargin(17.00787353515625);
            ps.setBottomMargin(28.346435546875);
            ps.setLeftMargin(17.00787353515625);
            ps.setRightMargin(28.34649658203125);

            // Create box
//            Shape box = documentBuilder.insertShape(ShapeType.TEXT_BOX, 91.1971853661725, 17.253521446167955);
            Shape box = new Shape(doc, ShapeType.TEXT_BOX);
            box.setWidth(153.0708661417323);
            box.setHeight(2.0);
            box.setWrapType(WrapType.NONE);
            box.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            box.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
            box.appendChild(new Paragraph(documentBuilder.getDocument()));
            documentBuilder.insertNode(box);

            box.setFillColor(new Color(255, 255, 255));
            box.getFill().setOpacity(0.6); 
            box.setStrokeColor(new Color(128, 128, 128));

            TextBox textBox = box.getTextBox();
            textBox.setInternalMarginRight(0);
            textBox.setInternalMarginLeft(0);
            textBox.setInternalMarginTop(0);
            textBox.setInternalMarginBottom(0);

            box.setStroked(false);
            box.setTop(160.76109494180463);
            box.setLeft(104.92957648329849);
            box.setZOrder(3);

            final Shape imageShape = documentBuilder.insertImage(getImage().getBytes(), 153.0708661417323, 2.0);
            imageShape.setZOrder(4);
            final Paragraph lastParagraph = box.getLastParagraph();
            lastParagraph.getParagraphBreakFont().setSize(1);
            lastParagraph.appendChild(imageShape);


            OoxmlSaveOptions options = new OoxmlSaveOptions();
            options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
            documentBuilder.getDocument().save(System.getProperty("user.dir") + File.separator + "with_option.docx", options);

            documentBuilder.getDocument().save(System.getProperty("user.dir") + File.separator + "without_option.docx");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static String getImage() {
        return "<?xml version=\"1.0\"?>\n" +
                "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'\n" +
                "          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>\n" +
                "<svg style=\"stroke-dasharray:none; shape-rendering:auto; font-family:'Dialog'; text-rendering:auto; fill-opacity:1; color-interpolation:auto; color-rendering:auto; font-size:12px; fill:black; stroke:black; image-rendering:auto; stroke-miterlimit:10; stroke-linecap:square; stroke-linejoin:miter; font-style:normal; stroke-width:1; stroke-dashoffset:0; font-weight:normal; stroke-opacity:1;\" xmlns=\"http://www.w3.org/2000/svg\" width=\"116.43423260617014pt\" contentScriptType=\"text/ecmascript\" preserveAspectRatio=\"xMidYMid meet\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" zoomAndPan=\"magnify\" version=\"1.0\" contentStyleType=\"text/css\" height=\"7.2pt\"\n" +
                "><defs id=\"genericDefs\"\n" +
                "  /><g\n" +
                "  ><defs id=\"defs1\"\n" +
                "    ><clipPath clipPathUnits=\"userSpaceOnUse\" id=\"clipPath1\"\n" +
                "      ><path d=\"M0 0 L167 0 L167 14 L0 14 L0 0 Z\"\n" +
                "      /></clipPath\n" +
                "    ></defs\n" +
                "    ><g style=\"stroke-linecap:butt; font-size:10px; text-rendering:geometricPrecision; image-rendering:optimizeQuality; color-rendering:optimizeQuality; font-family:sans-serif; stroke-linejoin:bevel; stroke-dasharray:6,6; color-interpolation:linearRGB; stroke-width:0.6; stroke-miterlimit:1;\"\n" +
                "    ><line x1=\"6.9296\" x2=\"160.0005\" y1=\"6.7611\" style=\"fill:none; clip-path:url(#clipPath1);\" y2=\"6.7611\"\n" +
                "    /></g\n" +
                "  ></g\n" +
                "></svg\n" +
                ">\n";
    }
}

Update:
problem solved by setting image font:
imageShape.getFont().setSize(1);

@aolo23

It is nice to hear from you that your problem has been solved. Please let us know if you have more queries.