26

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.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
PSR
  • 39,804
  • 41
  • 111
  • 151

7 Answers7

73

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.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
6

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);
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
5
 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();
}
shehzad lakhani
  • 495
  • 5
  • 14
  • What is the advantage of your solution compared to the much simpler solution in the accepted answer? – mkl Mar 08 '17 at 10:22
  • *"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. – mkl Mar 14 '17 at 08:08
  • you'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 – shehzad lakhani Mar 14 '17 at 08:53
  • `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*? – mkl Mar 14 '17 at 10:02
5

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;
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Andy Brown
  • 5,309
  • 4
  • 34
  • 39
3

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);
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
0

This is what worked for me (itext 5.0.5 ):

Paragraph p3= new Paragraph("Hello" );
  p3.setAlignment(Element.ALIGN_CENTER);
oDev
  • 23
  • 3
0

I have searching solution for this to align PdfPCell text into right and also center. After modify and change the code sequence it is work.

This code is not work to align text to center.

              PdfPCell cell = new PdfPCell();
              cell.addElement(new Phrase("Testing Page");
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);

After modifying code with this , it is working now.

              Paragraph p = new Paragraph("Testing Page");
              //Pass Paragraph object into PdfPCell 
              PdfPCell cell = new PdfPCell(p);
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);
Saket Yadav
  • 967
  • 1
  • 10
  • 23