1

I am trying to generate a pdf using iText and java and following is the code used but i am not able to align the text at the center of the PdfPCell, though i have gone through all links on google and tried setting setHorizontalAlignment(Element.ALIGN_CENTER); but this doesn't work for me.

Table with Cells:

PdfPTable heading1=new PdfPTable(3);

            try{

            heading1.setWidths(new int[]{1,1,1});
            heading1.setTotalWidth(527);
            heading1.setLockedWidth(true);
            heading1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            heading1.getDefaultCell().setFixedHeight(40);

            /* PdfPCell heading1Text1 = new PdfPCell();
             heading1Text1.setBorder(Rectangle.NO_BORDER);
             heading1Text1.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 12))); 
             heading1Text1.setUseAscender(true);
             heading1Text1.setHorizontalAlignment(Element.ALIGN_CENTER);
             heading1.addCell(heading1Text1);*/

             PdfPCell heading1Text2 = new PdfPCell();
             heading1Text2.setColspan(3);
             heading1Text2.setBorder(Rectangle.NO_BORDER);
             heading1Text2.setHorizontalAlignment(Element.ALIGN_CENTER);
             heading1Text2.addElement(new Phrase("RECEIPT", new Font(Font.FontFamily.HELVETICA, 12,Font.BOLD))); 
             heading1.addCell(heading1Text2);
            /* PdfPCell heading1Text3 = new PdfPCell();
             heading1Text3.setBorder(Rectangle.NO_BORDER);
             heading1Text3.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 12))); 
             heading1.addCell(heading1Text3);*/

                // write content

                document.add(heading1);

        }catch(DocumentException d){
            throw new ExceptionConverter(d);
        } 
Guneet Kaur
  • 544
  • 1
  • 8
  • 26
  • This question has been answered hundreds of times before. The behavior is described in the documentation and in the FAQ. I don't understand why people are still making this mistake. This "wrong use" of old iText versions is one of the reasons why we **rewrote iText from scratch** with iText 7 as result. Why don't you upgrade to iText 7 so that there is no more confusion between *text mode* and *composite mode*??? – Bruno Lowagie Nov 01 '17 at 14:26
  • @BrunoLowagie : Glad that the Original developer himself is commenting. Thanks & I'll try with the latest version. :) – Guneet Kaur Nov 02 '17 at 12:04

0 Answers0