Reputation: 18594
I'm trying to set the alignment of a paragraph to right.
This is what I tried, without any results:
Paragraph paragraph1 = new Paragraph(str1);
paragraph1.setHorizontalAlignment(HorizontalAlignment.RIGHT);
document.add(paragraph1);
I've already found lots of examples, but they all seem to deal with cells in tables. In my case it's just a paragraph.
Upvotes: 0
Views: 562
Reputation: 2458
Please try setTextAlignment method:
Paragraph paragraph1 = new Paragraph(str1);
paragraph1.setTextAlignment(TextAlignment.RIGHT);
document.add(paragraph1);
It's important to mention that setHorizontalAlignment and setTextAlignment methods have different goals. The former is about placing the paragraph itself (as an element) and the latter is about placing its content.
To see the difference one can set width on a paragraph and add that paragraph to a div element.
I've created a small test to demonstrate it:
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdfDocument);
Div div = new Div()
.setWidth(500)
.setBackgroundColor(ColorConstants.YELLOW);
Paragraph paragraph = new Paragraph("Hello World!")
.setTextAlignment(TextAlignment.CENTER)
.setHorizontalAlignment(HorizontalAlignment.RIGHT)
.setWidth(300)
.setBackgroundColor(ColorConstants.BLUE);
div.add(paragraph);
document.add(div);
As you can see, here I've created a paragraph with horizontal alignment set as RIGHT and text alignment set as CENTER.
The result looks the next way:
Upvotes: 3