How can I align/justify text when placing it on a page?

Q:

I am adding new text to an existing page (as described in the
following FAQ - http://www.pdftron.com/net/faq.html#how_watermark) and
would like to align or justify text in a given text box.

How can I align or justify text when placing it on the page?
-----

A:

You can align/justify the text by taking into account the length of
the text run (returned by element.GetTextLength()). For example:

In C#

writer.WriteElement(element_builder.CreateTextBegin(Font.Create(doc,
font), font_size));
Element element = element_builder.CreateTextRun("My text");

// To right justify
element.SetTextMatrix(1, 0, 0, 1, box_width - element.GetTextLength(),
pos_Y);

// To 'center' justify
// element.SetTextMatrix(1, 0, 0, 1, (box_width -
element.GetTextLength())/2, pos_Y);

writer.WriteElement(element);
writer.WriteElement (element_builder.CreateTextEnd())

In C++:

writer.WriteElement(element_builder.CreateTextBegin(Font::Create(doc,
font), font_size));
Element* element = element_builder.CreateTextRun("My Text");

// To right justify
element->SetTextMatrix(1, 0, 0, 1, box_width - element-

GetTextLength(), pos_Y);

// To 'center' justify
// element->SetTextMatrix(1, 0, 0, 1, (box_width - element-

GetTextLength())/2, pos_Y);

writer.WriteElement(element);
writer.WriteElement(element_builder->CreateTextEnd())