TextFrament alignment in Rectangle

Hi,

I’m trying to figure out how to get a text left alignment in a rectangle.

By using the example on https://github.com/aspose-pdf/Aspose.Pdf-for-Java/blob/master/Examples/src/main/java/com/aspose/pdf/examples/AsposePdfExamples/Graphs/CreateFilledRectangleObject.java as starting point, I have hadded a textfragment with setText and tried to left align with the setHorizontalAlignment and TextFragmentState’s setHorizontalAlignment property.

Setting the horizontal alignment seems to do nothing at all, is this correct? If so, what is the proper method of aligning the TextFragment in a Rectangle?

public class App 
{
    public static void main( String[] args )
    {
        Document doc = new Document();
        Page page = doc.getPages().add();
        Graph graph = new Graph(100, 400);
        page.getParagraphs().add(graph);
        Rectangle rect = new Rectangle(100, 100, 200, 120);

        TextFragment textFragment = new TextFragment("This is a test");
        textFragment.setHorizontalAlignment(HorizontalAlignment.Left);
        textFragment.getTextState().setHorizontalAlignment(HorizontalAlignment.Left);
        rect.setText(textFragment);

        graph.getShapes().add(rect);

        doc.save("Rect_text_test.pdf");
    }
}

Attached is the test project:

pdf.zip (1.6 KB)

– Sebas

@usernameispointlessu

Thank you for contacting support.

I have worked with the data shared by you and have been able to notice the problem with Horizontal Alignment in a rectangle. However, text alignment is working fine in case of simple paragraphs in a PDF file. A ticket with ID PDFJAVA-37400 has been logged in our issue management system for further investigation and resolution. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.

We are sorry for the inconvenience.

@usernameispointlessu,
As a workaround, you can place a floating box in the specific region of the page, and then add the text fragment node into the floating box. The floating box accepts absolute positioning rather than flow layout (Top left to Bottom right) on the page. You can use Top, Left, Bottom and Right properties to adjust the position of a floating box. Please try the following code:

[Java]

String dataDir = "C:\\Pdf\\test602\\";
// Open document
Document pdfDocument = new Document();
com.aspose.pdf.Page page = pdfDocument.getPages().add();
FloatingBox box = new FloatingBox();
box.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, Color.getBlack()));
TextFragment fragment = new TextFragment("Main Text");
fragment.setMargin(new MarginInfo(0, 20, 0, 0));
fragment.getTextState().setHorizontalAlignment(HorizontalAlignment.Justify);
box.getParagraphs().add(fragment);
page.getParagraphs().add(box);
pdfDocument.save(dataDir + "Test_Out.pdf"); 

This is the output PDF: Test_Out.pdf (1.9 KB)

@Farhan.Raza Thanks.

@imran.rafique Looks good, I will work around the aforementioned issue with your suggestion, thanks.