We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Text alignment within a Rectangle does not work (all versions since 19.10)

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)

@PeteLee

We have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as PDFNET-41109. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.