It is possible to add some text to a Rectangle element, but Aspose.Pdf seems to reject any text alignment defined for the text. For instance, it seems there is no way to force it to align to the left.
Sample code below:
public class TestClass {
private static final String ABSOLUTE_FILEPATH = "text-alignment-test.pdf";
public static void main(String[] args) throws Exception {
Document document = new Document();
Page page = document.getPages().insert(1);
MarginInfo pageMargin = page.getPageInfo().getMargin();
Graph graph = new Graph(
(float) (page.getPageInfo().getWidth() - pageMargin.getLeft() - pageMargin.getRight()),
(float) (page.getPageInfo().getHeight() - pageMargin.getTop() - pageMargin.getBottom()));
page.getParagraphs().add(graph);
Rectangle drawRect = new Rectangle(50f, 250f, 250f, 120f);
drawRect.setText(new TextFragment("This should NOT be centered!"));
drawRect.getText().setVerticalAlignment(VerticalAlignment.Top);
drawRect.getText().setHorizontalAlignment(HorizontalAlignment.Left);
// drawRect.getText().getTextState().setHorizontalAlignment(HorizontalAlignment.Left);
// ^^^ this above does not work either
graph.getShapes().add(drawRect);
document.save(ABSOLUTE_FILEPATH);
}
}
The output looks consistently like this:
Screen Shot 2021-12-07 at 6.23.48 PM.jpg (42.5 KB)