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!
Answer
Class com.itextpdf.layout.element.Paragraph
in itext7
has method setTextAlignment
. I hope this is what you are looking for:
... paragraph1.setTextAlignment(TextAlignment.RIGHT); ...