Cannot apply vertical alignment for Shape TextBox

I tried both shape.setVerticalAlignment() and shape.getTextBox().setVerticalAnchor()
But the text doesn’t get center aligned vertically.
I am using Aspose.Words 23.5 version for java.

GroupShape g1 = new GroupShape(document);
g1.setBounds(new Rectangle2D.Float(0f, 0f, (float)builder.getPageSetup().getPageWidth(), (float)builder.getPageSetup().getPageHeight()));
g1.setCoordSize(new Dimension((int)builder.getPageSetup().getPageWidth(), (int)builder.getPageSetup().getPageHeight()));
g1.setCoordOrigin(new Point(0, 0));
document.getLastSection().getBody().getLastParagraph().appendChild(g1);

Shape s1 = new Shape(document, ShapeType.TEXT_BOX);
Paragraph p1 = new Paragraph(document);
s1.appendChild(p1);
Run r1 = new Run(document);
r1.setText("Hello");
p1.appendChild(r1);

s1.setHeight(200);
s1.setWidth(200);
s1.setTop(50);
s1.setLeft(20);

s1.setVerticalAlignment(VerticalAlignment.CENTER);
s1.getTextBox().setVerticalAnchor(TextBoxAnchor.MIDDLE);

g1.appendChild(s1);

@aishwarya12joshi Vertical alignment of textboxes are not available in older versions of MS Word. So to make it work you should optimize the document for newer version of MS Word. For example see the following code:

Document doc = new Document();

Shape s1 = new Shape(doc, ShapeType.TEXT_BOX);
Paragraph p1 = new Paragraph(doc);
s1.appendChild(p1);
Run r1 = new Run(doc, "Hello");
p1.appendChild(r1);

s1.setHeight(200);
s1.setWidth(200);
s1.setTop(50);
s1.setLeft(20);

s1.setVerticalAlignment(VerticalAlignment.CENTER);
s1.getTextBox().setVerticalAnchor(TextBoxAnchor.MIDDLE);

doc.getFirstSection().getBody().getFirstParagraph().appendChild(s1);

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);
doc.save("C:\\Temp\\out.docx");

Thanks a lot. This worked.

Verified that only verticalAnchor setting is required

s1.getTextBox().setVerticalAnchor(TextBoxAnchor.MIDDLE);

Also in our case we don’t know which MS Word version will customer have 2010/2016/2019 and if we add WORD_2019 will it cause any issue for older versions while rendering some other document things?

document.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);

@aishwarya12joshi CompatibilityOptions.OptimizeFor method sets the appropriate compatibility settings in the document. This will not cause any issues for older versions of MS Word. An older version will simply not use the options, which are not supported.

Okay. Thank you for the quick response.

1 Like

When I merge multiple word document files the table data gets shifted to first page. Wrong display of data is shown if I add compatibility to merge document file.

Document document = new Document();

for(String fileName : wordFileList) {
	Document sourceDoc = new Document(fileName);
	document.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}

document.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);		
document.updateFields();

For all document files I have set compatibility as MsWordVersion.WORD_2019. But after merge vertical alignmnet was not working. So I tried adding compatibility as MsWordVersion.WORD_2019 to merged document. But it fails to render the data correctly.

All files have been created using GroupShape and Shape.

@aishwarya12joshi Your documents might have been created using different versions of MS Word and as a result have different set of compatibility options. When you concatenate the documents compatibility options of the main document are applied and the content is reflowed appropriately. This might lead to difference in layout of the appended documents. Unfortunately, there is no way to keep different compatibility options for different parts of the document.

You can try concatenating document using Merger class with MergeFormatMode.KEEP_SOURCE_LAYOUT options:

com.aspose.words.Merger.merge("C:\\Temp\\out.docx",
        new String[] { "C:\\Temp\\in1.docx", "C:\\Temp\\in2.docx" },
        SaveFormat.DOCX,
        MergeFormatMode.KEEP_SOURCE_LAYOUT);

I am currently using Aspose.Words 23.5 version.
I cannot find Merger class in this jar. In which version was this introduced?

@aishwarya12joshi Merger class has been introduced in 23.6 version of Aspose.Words:
https://releases.aspose.com/words/java/release-notes/2023/aspose-words-for-java-23-6-release-notes/