1

I'm using itextpdf to create my pdf with tables. While creating table i need to align some column to right, but its now working properly , can you guys help me.

I tried googling too, but didt work out for me. im using itextpdf 5.4 version.

 public void generateMonthlySubReport(String[][] StrArray,String dueMonth,int Amt){
    try {

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(MON_SUB_FILE));         
        PdfPTable pt = new PdfPTable(StrArray[0].length); 
        pt.setTotalWidth(new float[]{ 55,120,360,140});
        pt.setLockedWidth(true);
        PdfPCell pcell = new PdfPCell();
        document.open();                        
        addKvLogo(document); 
        Chunk glue = new Chunk(new VerticalPositionMark());
        Paragraph p1 = new Paragraph("Monthly Subscription Report",catFont);
        p1.setAlignment(Element.ALIGN_CENTER);
        addEmptyLine(p1,2);
        document.add(p1);
        Paragraph p2 = new Paragraph("Month : "+dueMonth);
        p2.add(new Chunk(glue));
        p2.add("Per Member : Rs."+Amt);        
        addEmptyLine(p2,2);
        document.add(p2);

        for(int i=0;i<StrArray.length;i++){
            for(int j=0;j<StrArray[i].length;j++){ 
                pcell = new PdfPCell();
                if(i==0){
                    pcell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                }else{
                    pcell.setBackgroundColor(BaseColor.WHITE);
                }                    
                pcell.setUseAscender(true);
                pcell.setMinimumHeight(22);
                pcell.setPaddingLeft(10);                    
                pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                pcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                pcell.addElement(new Phrase(StrArray[i][j]));
                pt.addCell(pcell);
            }
        }            
        pt.setTotalWidth(PageSize.A4.getWidth()-(document.leftMargin()*2));
        pt.setLockedWidth(true);
        document.add(pt);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }        

}             `
Pradeep
  • 51
  • 1
  • 8
  • Somebody voted to close the question, and also gave you a down-vote. However, this is a genuine question, hence my up-vote. I would recommend to read the documentation in the future, though, because what I wrote in my answer is documented in many places. – Bruno Lowagie Apr 15 '17 at 12:45

1 Answers1

3

You are mixing text mode with composite mode.

This is text mode:

pcell = new PdfPCell(new Phrase(StrArray[i][j]));
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);

In this case, the alignment of the cell will be used for the alignment of the text.

This is composite mode:

pcell = new PdfPCell();
Paragraph p = new Parapgraph(StrArray[i][j])
p.setAlignment(Element.ALIGN_RIGHT);
pcell.addElement(p);

In this case, the alignment of the cell is ignored, in favor of the alignment of the element.

How to know the difference between text mode and composite mode?

iText automatically switches from text mode to composite mode in a PdfPCell the moment you use the addElement() method. As soon as you do this, some properties defined at the cell level are ignored. This explains why the content you are adding isn't right-aligned.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165