Align Paragraph at the center of the page
80,648
Solution 1
Use Paragraph#setAlignment(int)
:
Paragraph preface = new Paragraph();
preface.setAlignment(Element.ALIGN_CENTER);
See the ALIGN_*
constants in the Element
interface for more possible values.
Solution 2
Not sure if this is an old version, but for PdfWriter these methods weren't there. Instead I used:
Paragraph p = new Paragraph("This too shall pass");
p.Alignment = Element.ALIGN_CENTER;
Solution 3
public static final String DEST = "results/tables/centered_text.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CenteredTextInCell().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph para = new Paragraph("Test", font);
para.setLeading(0, 1);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(50);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.addElement(para);
table.addCell(cell);
document.add(table);
document.close();
}
Solution 4
If you are looking for a solution to Itext7 then you can use the method setTextAlignment(...)
.
Example:
Paragraph preface = new Paragraph();
// add text
preface.setTextAlignment(TextAlignment.CENTER);
Solution 5
If any one is looking for .NET/C# version, below is how I achieved the CENTER alignment.
I am using iText7 library for .NET/C#, and I achieved this using :
Paragraph preface = new Paragraph();
preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
Related videos on Youtube
![PSR](https://i.stack.imgur.com/XKb9Q.jpg?s=256&g=1)
Comments
-
PSR almost 3 years
I am using itext to generate pdf file. I want to align my title in the middle of the page. Presently i am using like this
Paragraph preface = new Paragraph(); for (int i = 0; i < 10; i++) { preface.add(new Paragraph(" ")); }
Is it correct or is there any another best way to do this.
-
mkl over 7 yearsWhat is the advantage of your solution compared to the much simpler solution in the accepted answer?
-
mkl over 7 years"The following line doesn't really make sense:
p1.setAlignment(Element.TABLE)
" - I don't see that code anywhere here, neither in the question nor in any answer. -
shehzad lakhani over 7 yearsyou're using a version of iText that is really, really old. The following line doesn't really make sense: preface.setAlignment(Element.ALIGN_CENTER); so use this method instead above older itext version cell.setVerticalAlignment(Element.ALIGN_MIDDLE); this method much faster and better
-
mkl over 7 years
Paragraph.setAlignment
is still present in the most current iText 5.5.x versions, and your code definitively cannot claim being iText 7 code. So why do you claim that I am using a version of iText which is really, really old? Furthermore, I have not timed the methods, so I cannot say your claim that your method is faster is wrong. But based on what do you claim it is better?