Tiger
Tiger

Reputation: 39

Itext pdf - text alignment to right

I am using Itext PDF API to generate a pdf. I am trying to get some text to be aligned to the right-hand side of the pdf. I have tried the manual method of spacing but is not working for some reason (Code shown below). Meanwhile, if there is a way of doing it dynamically that would be great, please!

String dest = "\\location\\";
    PdfWriter writer;

        writer = new PdfWriter(dest);

        // Creating a PdfDcoument
        PdfDocument pdf = new PdfDocument(writer);

        // Creating a Document
        Document document = new Document(pdf);

        // Creating a String
        String para1 = "TEXT";

        //Spacing length
        while (para1.length() < 50) {

              para1 = " " + para1;

            }


        //Creating Paragraphs
        Paragraph paragraph1 = new Paragraph(para1);

        //paragraph1.setAlignment(Element.ALIGN_CENTER);

        //Adding Paragraphs to document
        document.add(paragraph1);

        // Closing the document
        document.close();

Thanks in advance!

Upvotes: 3

Views: 11681

Answers (2)

F. P. Freely
F. P. Freely

Reputation: 1124

I'm using com.itextpdf:itextpdf:5.5.10 and it looks like the stuff has moved around a bit.

    paragraph1.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT)

Upvotes: 2

mistyk
mistyk

Reputation: 84

Class com.itextpdf.layout.element.Paragraph in itext7 has method setTextAlignment. I hope this is what you are looking for:

...                 
            paragraph1.setTextAlignment(TextAlignment.RIGHT);    
...

Upvotes: 6

Related Questions