Vertical align of text in a textbox LibreOffice

I tried this code but I can’t seem to be able to make it work, on libreoffice I just add a textbox and tell it to vertical align, but I’ve tried set layout flow to vertical, paragraph format to center, and nothing has worked, not even this internalMargin.
I’m not allowed to download the zip but I believe it’s the same issue:
image.png (7.0 KB)

@daniel.nicoletti You can use TextBox.VerticalAnchor to specify vertical position of text in textbox. But this feature seems to be not supported by LibreOffice

Document doc = new Document();

Shape textBox = new Shape(doc, ShapeType.TextBox);
            
textBox.Width = 200;
textBox.Height = 200;
textBox.TextBox.VerticalAnchor = TextBoxAnchor.Middle;

Paragraph para = new Paragraph(doc);
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
para.AppendChild(new Run(doc, "This is centered text in textbox"));
textBox.AppendChild(para);
doc.FirstSection.Body.FirstParagraph.AppendChild(textBox);

// The vertical aligning of text inside text boxes is available from Microsoft Word 2007 onwards.
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2007);
doc.Save(@"C:\Temp\out.docx");

here is document produced by this code: out.docx (7.0 KB)
In MS Office position of the shape is correct, but in LibreOffice it is not retained.

Could you please let us know what is your target format? Also, please attach your input and expected output documents.

My target output is 2007-365 DOCX, I wasn’t expecting that this simple feature was not supported, using LibreOffice like my screenshot also opens fine on MS Word.

I’m inserting HTML text on the textbox but even plain text didn’t work, it thrown an error, here is my code that almost works:

auto       shape = builder->InsertShape(ShapeType::TextBox, ConvertUtil::MillimeterToPoint(box.width()), ConvertUtil::MillimeterToPoint(box.height()));
auto previousNode = builder->get_CurrentParagraph();
auto textBox = shape->get_TextBox();
textBox->set_VerticalAnchor(Aspose::Words::Drawing::TextBoxAnchor::Middle);
// Your code, without HTML and that thown
// terminate called after throwing an instance of 
// 'System::ExceptionWrapper<System::Details_ArgumentException>'
/// what():  System::ArgumentException: Cannot insert a node of this type at this location.
auto paragraph = System::MakeObject<Aspose::Words::Paragraph>(builder->get_Document());
paragraph->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
auto run = System::MakeObject<Aspose::Words::Run>(builder->get_Document());
run->set_Text(u"centered");
shape->AppendChild(run);
// Current code
builder->MoveTo(shape->get_FirstParagraph());
builder->InsertHtml(System::String(toHtml().toStdU16String()));
builder->MoveTo(previousNode);

@daniel.nicoletti The problem occurs because in your code you are inserting Run not into a Shape, which is not correct. See the following line of code:

shape->AppendChild(run);

You should insert a paragraph into a shape and run into the paragraph. Please see the following code:

auto paragraph = System::MakeObject<Aspose::Words::Paragraph>(builder->get_Document());
paragraph->get_ParagraphFormat()->set_Alignment(Aspose::Words::ParagraphAlignment::Center);
auto run = System::MakeObject<Aspose::Words::Run>(builder->get_Document());
run->set_Text(u"centered");
shape->AppendChild(paragraph);
paragraph->AppendChild(run);
1 Like

Worked thanks, but I also needed to explicit call OptimizeFor(…)

@daniel.nicoletti Yes, it is required because the vertical alignment of text inside text boxes is available from starting from Microsoft Word 2007 onwards.