Q: I would like to insert text and images with specific horizontal
alignment like center. Is
that possible?
----
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())
To center align images you only need to make sure that the translation
component in the transformation matrix is set to a correct location.
Matrix2D mtx = Matrix2D(width, 0, 0, height, center_x - width/2.0,
center_y + height/2.0); Element element = f.CreateImage(img,
mtx); ....