I am new to itext and i am facing issue in displaying page number as " 1 of
1". 1 of is getting printed but total number of page is not getting printed.

If i open the pdf with PDF Vista its showing but in Adobe Reader its not
displaying. If i copy the blank area from pdf and paste it in notepad its
showing me total number of pages. So i think there is some issue with
displaying of it.

I wanted help on this urgently. I am also attaching my code here with this
post.
Any help is greatly appreciated.

package com.cibavision.mcv.web.accountmanagement.util;

import java.awt.Color;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cibavision.mcv.business.accountmanagement.model.InvoiceLine;
import com.cibavision.mcv.business.accountmanagement.model.InvoiceTax;
import com.cibavision.mcv.common.exception.ApplicationException;
import com.cibavision.mcv.web.accountmanagement.viewbeans.InvoiceViewBean;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.mindtree.momentum.api.i18n.I18nServiceProvider;
import com.mindtree.momentum.bdm.locale.MomentumLocale;


public class InvoicePdfGenerator extends PdfPageEventHelper  {

        private static final Log logger =
LogFactory.getLog(InvoicePdfGenerator.class);
        private List invoiceList = null;
        private String urlString = null;
        /** A template that will hold the total number of pages. */
        public PdfTemplate template;
        /** The font that will be used. */
        public BaseFont helv;
        private float headerHeight = 250f;
        private int pageCount = 0;
        float top = 0f;
        float right = 0f;

        public InvoicePdfGenerator()
        {

        }

        public InvoicePdfGenerator(List invoices,String url)
        {
                invoiceList = invoices;
                urlString = url;
        }

        /**
         * @see
com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter,
com.lowagie.text.Document)
         */
        public void onOpenDocument(PdfWriter writer, Document document) {
                try {
                        // initialization of the template
                        template = 
writer.getDirectContent().createTemplate(100, 0);
                        template.setBoundingBox(new Rectangle(-20, -10, 100, 
0));
                        // initialization of the font
                        helv = BaseFont.createFont("Helvetica", 
BaseFont.WINANSI, false);
                }
                catch(Exception e) {
                        throw new ExceptionConverter(e);
                }
        }    
        /**
         * @see
com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter,
com.lowagie.text.Document)
         */
        public void onCloseDocument(PdfWriter writer, Document document) {
                template.beginText();
        template.setFontAndSize(helv,8);
        template.setTextMatrix(500,747);
        template.showText(""+  (writer.getPageNumber() - 1));
        template.endText();
        }

        /**
         * @see
com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
com.lowagie.text.Document)
         */
        public void onEndPage(PdfWriter writer, Document document) {
                try {
                        PdfContentByte pdfByte = null;
                        InvoiceViewBean invoiceViewBean = null;
                        Iterator it = null;
//                      PdfPCell cell = null;
                        PdfPTable headerTable = null;
                        PdfPTable topTable = null;
                        PdfPTable topRightHeader = null;
                        PdfPTable invoiceRightHeader = null;
                        PdfPTable middleTable = null;
                        PdfPTable bottomTable = null;

                        Font font1,font2,font3;

                        font1 = new Font(Font.HELVETICA,8, Font.BOLD);
                        font2 = new Font(Font.HELVETICA,8, Font.NORMAL);
                        font3 = new Font(Font.HELVETICA,10,Font.BOLD);

                        PdfContentByte cb = writer.getDirectContent();
                        cb.saveState();
//                      Rectangle page = null;

                        pdfByte = new PdfContentByte(writer);
                        headerTable = new PdfPTable(1);
//                      
headerTable.getDefaultCell().setBorderColor(Color.WHITE);

//                      page = document.getPageSize();

                        it = invoiceList.iterator();
                        invoiceViewBean = (InvoiceViewBean) it.next();

//                      PdfPTable logormtTable = new PdfPTable(1);
                        float[] logowidths = {0.375f,0.325f,0.30f};
                        PdfPTable logoTable = new PdfPTable(logowidths);

                        Image image = Image.getInstance(new 
java.net.URL(urlString));
                        //image.setAbsolutePosition(50.0f, 50.0f);
                        //image.scalePercent(50, 20);
                        image.scalePercent(65);
                        
                        PdfPCell cell = null;
                        Rectangle page = document.getPageSize();


//                      = new PdfPCell(image);  
//                      cell.setBorder(Rectangle.NO_BORDER);
//                      logormtTable.addCell(cell);
//                      writer.setStrictImageSequence(true);
//                      Code For I18N
//                      I18nServiceProvider kk = new I18nServiceProvider();
//                      MomentumLocale mm = new MomentumLocale();
//                      
mm.setLanguage(invoiceViewBean.getLanguage().toLowerCase());
//                      if (invoiceViewBean.getLanguage().equals("EN"))
//                      mm.setCountry("US");
//                      if (invoiceViewBean.getLanguage().equals("FR"))
//                      mm.setCountry("FR");
                        PdfPTable remitTable = new PdfPTable(1);
                        String remitAddress="";

                        cell =new PdfPCell(new Paragraph("REMIT TO ",font1));
                        cell.setBorder(Rectangle.NO_BORDER);
                        remitTable.addCell(cell);


                        if (invoiceViewBean.getRemitTo() != null)
                        { 

                                String []remitText = 
invoiceViewBean.getRemitTo().split(":");

                                if (remitText.length == 5)
                                        //cell = new PdfPCell(new 
Paragraph("REMIT TO:
"+remitText[0]+"\n"+remitText[1]+"\n"+remitText[2]+"\n"+remitText[3]+"\n"+remitText[4],font2));
                                
remitAddress=remitText[0]+","+remitText[1]+"\n"+remitText[2]+"\n"+remitText[3];
                                if (remitText.length == 4)
                                        //cell = new PdfPCell(new 
Paragraph("REMIT TO:
"+remitText[0]+"\n"+remitText[1]+"\n"+remitText[2]+"\n"+remitText[3],font2));
                                
remitAddress=remitText[0]+","+remitText[1]+"\n"+remitText[2]+"\n"+remitText[3];
                                if (remitText.length == 3)
                                        //cell = new PdfPCell(new 
Paragraph("REMIT TO:
"+remitText[0]+"\n"+remitText[1]+"\n"+remitText[2],font2));
                                        
remitAddress=remitText[0]+","+remitText[1]+"\n"+remitText[2];
                                if (remitText.length == 2)
                                        //cell = new PdfPCell(new 
Paragraph("REMIT TO:
"+remitText[0]+"\n"+remitText[1],font2));
                                        
remitAddress=remitText[0]+","+remitText[1];
                                if (remitText.length == 1)
                                        //cell = new PdfPCell(new 
Paragraph("REMIT TO: "+remitText[0],font2));
                                        remitAddress=remitText[0];
                                cell=new PdfPCell(new 
Paragraph(remitAddress,font2));
                                cell.setBorder(Rectangle.NO_BORDER);
                                remitTable.addCell(cell);
                        }
                        PdfPCell logorm = new PdfPCell(remitTable);     
                        logorm.setBorder(Rectangle.NO_BORDER);
                        logoTable.addCell(logorm);      


                        
                        if (invoiceViewBean.getHeaderText() != null)
                        {
                                //System.out.println("Header Text 
----------------------------
"+invoiceViewBean.getHeaderText());
                                String []headerText = 
invoiceViewBean.getHeaderText().split(":");
                                //System.out.println("Header Text Length 
----------------
"+headerText.length);

                                if (headerText.length == 3)
                                        cell = new PdfPCell(new
Paragraph(headerText[0]+"\n"+headerText[1]+"\n"+headerText[2],font2));
                                if (headerText.length == 2)
                                        cell = new PdfPCell(new
Paragraph(headerText[0]+"\n"+headerText[1]+"\n"+"\n",font2));
                                if (headerText.length == 1)
                                        cell = new PdfPCell(new
Paragraph(headerText[0]+"\n"+"\n"+"\n"+"\n",font2));
                                cell.setBorder(Rectangle.NO_BORDER);
                                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                //addressTable.addCell(cell);
                        }
                        else
                                cell = new PdfPCell(new Paragraph("  "));
                        cell.setBorder(Rectangle.NO_BORDER);
                        logoTable.addCell(cell);

//                      document.add(logoTable);
                        PdfPTable invmTable = new PdfPTable(1);

//                      Printing the header by checking whether it is a 
standard invoice or a
credit memo
//                      F2 - Invoice G2-Credit Memo

                        PdfPTable invTable = new PdfPTable(2);                  
                         
//                      invTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invTable.setWidthPercentage(0);                         
 

                        cell = new PdfPCell(new Paragraph("NUMBER",font1));
//                      cell = new PdfPCell(new 
Paragraph(kk.getLiteral("NUMBER", mm)));        
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setBorderWidth(0.0f);
                        cell.setBorderColor(Color.WHITE);
                        invTable.addCell(cell);         
                        cell = new PdfPCell(new
Paragraph(invoiceViewBean.getInvoiceNumber(),font2));   
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invTable.addCell(cell);
                        cell = new PdfPCell(new Paragraph("DATE",font1));
//                      cell = new PdfPCell(new Paragraph(kk.getLiteral("DATE", 
mm)));  
                        cell.setBorder(Rectangle.NO_BORDER);
                        invTable.addCell(cell); 
                        cell = new PdfPCell(new
Paragraph(invoiceViewBean.getInvoiceDate(),font2));     
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invTable.addCell(cell);
                        cell = new PdfPCell(new Paragraph("PAGE",font1));
//                      cell = new PdfPCell(new Paragraph(kk.getLiteral("PAGE", 
mm)));  
                        cell.setBorder(Rectangle.NO_BORDER);
                        invTable.addCell(cell);

                        right = cell.right() + 10f;
                        top = cell.TOP;


//                      add pagecount
//                      System.out.println(">>> page count"+pageCount);
                        String text = "   "+document.getPageNumber() + "  of  ";

                        cb.beginText();
                        float textSize = helv.getWidthPoint(text, 8);
                        float textBase = top;

                        cb.setFontAndSize(helv, 8);
                        cb.setTextMatrix(document.left(), textBase);
//                      cb.showText(text);
                        cb.endText();
                        cb.saveState();


                        cb.addTemplate(template, right + textSize, textBase);
                        cell = new PdfPCell(new Paragraph(text ,font2));        
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invTable.addCell(cell);
                        invmTable.addCell(invTable);    

                        PdfPCell invm = new PdfPCell(invmTable);
                        invm.setBorder(Rectangle.NO_BORDER);
                        logoTable.addCell(invm);        

                        logoTable.addCell(invmTable);
                        logoTable.setWidthPercentage(100);

//                      logoTable.setTotalWidth(page.getWidth() - 
document.leftMargin() -
document.rightMargin());
//                      logoTable.writeSelectedRows(0, -1, 
document.leftMargin(),
page.getHeight() - document.topMargin() + logoTable.getTotalHeight(),
writer.getDirectContent());

                        float[] imageWidth = {0.3f, 0.7f};
                        PdfPTable imageTable = new PdfPTable(imageWidth);
                        cell = new PdfPCell(image);
                        cell.setBorder(Rectangle.NO_BORDER);
                        imageTable.addCell(cell);

                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("F2"))
                        {
                                cell = new PdfPCell(new Paragraph("\n\n"+"      
                                                                   
I N V O I C E",font3));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("INVOICE", mm)));
                        }
                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("G2"))
                        {
                                cell = new PdfPCell(new Paragraph("             
                               
C R E D I T M E M O",font3));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("CREDITMEMO", mm)));
                        }
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        imageTable.addCell(cell);

                        cell = new PdfPCell(imageTable);
                        cell.setBorder(Rectangle.NO_BORDER);
                        headerTable.addCell(cell);


                        cell = new PdfPCell(logoTable);
                        cell.setBorder(Rectangle.NO_BORDER);
                        headerTable.addCell(cell);

//                      Address Table
                        float[] addresswidths = {0.35f, 0.30f, 0.35f};
                        PdfPTable addressTable = new PdfPTable(addresswidths);

                        PdfPTable payerTable = new PdfPTable(2);

                        String payerName="";
                        if(invoiceViewBean.getPayerName()!=null &&
invoiceViewBean.getPayerName().length()>0){
                                payerName=invoiceViewBean.getPayerName()+"\n\n";
                        }
                        else 
                                payerName="\n\n";

                        if ((invoiceViewBean.getPayer() != null ||
!invoiceViewBean.getPayer().equals("") )&& (invoiceViewBean.getBillTo() !=
null || !invoiceViewBean.getBillTo().equals("")))
                        {
                                if 
(invoiceViewBean.getPayer().equals(invoiceViewBean.getBillTo()))
                                {

                                        cell = new PdfPCell(new Paragraph(" 
",font1));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        payerTable.addCell(cell);

                                        cell = new PdfPCell(new Paragraph("  
"));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        payerTable.addCell(cell);

                                        cell = new PdfPCell(new Paragraph(" 
",font2));
                                        //cell = new PdfPCell(new 
Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        cell.setColspan(2);
                                        payerTable.addCell(cell);

                                        cell = new PdfPCell(payerTable);
                                }
                                else
                                {
                                        //cell = new PdfPCell(new 
Paragraph("PAYER"+"        "+
Integer.parseInt(invoiceViewBean.getPayer())+"\n"+payerName,font2));

                                        cell = new PdfPCell(new 
Paragraph("PAYER",font1));
                                        //cell = new PdfPCell(new 
Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        payerTable.addCell(cell);

                                        cell = new PdfPCell(new
Paragraph(""+Integer.parseInt(invoiceViewBean.getPayer()),font2));
                                        //cell = new PdfPCell(new 
Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        payerTable.addCell(cell);

                                        cell = new PdfPCell(new 
Paragraph(payerName,font2));
                                        //cell = new PdfPCell(new 
Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        cell.setColspan(2);
                                        payerTable.addCell(cell);
                    
                                        cell = new PdfPCell(payerTable);        
                                
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("PAYER", mm)+"       
"+ invoiceViewBean.getPayer()+"\n"+invoiceViewBean.getPayerName()));
                                }
                        }                       
                        else
                        {
                                cell = new PdfPCell(new Paragraph(" "));
                        }
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);        
             
                        addressTable.addCell(cell);
                        System.out.println("payer table height : 
"+payerTable.getTotalHeight());
                        
                        cell = new PdfPCell(new Paragraph("  "));
                        cell.setBorder(Rectangle.NO_BORDER);
                        if ((invoiceViewBean.getPayer() != null ||
!invoiceViewBean.getPayer().equals("") )&& (invoiceViewBean.getBillTo() !=
null || !invoiceViewBean.getBillTo().equals("")))
                        {
                                if 
(invoiceViewBean.getPayer().equals(invoiceViewBean.getBillTo()))
                                {
                        cell.setFixedHeight(32.0f);
                                }
                        }
                        addressTable.addCell(cell);
                                

                        PdfPTable soldTable = new PdfPTable(2);
                        
                        if ((invoiceViewBean.getSoldTo() != null||
!invoiceViewBean.getSoldTo().equals("")) && (invoiceViewBean.getShipTo() !=
null || !invoiceViewBean.getShipTo().equals("")))
                        {
                                if 
(invoiceViewBean.getSoldTo().equals(invoiceViewBean.getShipTo()))
                                {
                                        //cell = new PdfPCell(new Paragraph("  
"));
                                        //cell = new PdfPCell(new 
Paragraph("SOLD TO"+"    
"+invoiceViewBean.getSoldTo()+"\n"+invoiceViewBean.getSoldToName(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SOLDTO", mm)+"    
"+invoiceViewBean.getSoldTo()+"\n"+invoiceViewBean.getSoldToName()));
                                        cell = new PdfPCell(new Paragraph("  
",font1));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell = new PdfPCell(new Paragraph("  
",font2));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell=new PdfPCell(new Paragraph("   
",font2));
                                        cell.setColspan(2);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell = new PdfPCell(soldTable);
                                }
                                else
                                {
                                        if(invoiceViewBean.getSoldToName() == 
null ||
invoiceViewBean.getSoldToName().equals(""))
                                        {
                                                
invoiceViewBean.setSoldToName("---");
                                        }
                                        cell = new PdfPCell(new Paragraph("SOLD 
TO"+"    
"+Integer.parseInt(invoiceViewBean.getSoldTo())+"\n"+invoiceViewBean.getSoldToName(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SOLDTO", mm)+"    
"+invoiceViewBean.getSoldTo()+"\n"+invoiceViewBean.getSoldToName()));
                                        cell = new PdfPCell(new Paragraph("SOLD 
TO",font1));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell = new PdfPCell(new Paragraph(" 
"+invoiceViewBean.getSoldTo(),font2));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell=new PdfPCell(new
Paragraph(invoiceViewBean.getSoldToName(),font2));
                                        cell.setColspan(2);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        soldTable.addCell(cell);
                                        cell = new PdfPCell(soldTable);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                }
                        }
                        cell.setBorder(Rectangle.NO_BORDER);
//                      cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        addressTable.addCell(cell);
                        addressTable.setSpacingBefore(25f);
                        addressTable.setWidthPercentage(100);   
//                      addressTable.setTotalWidth(page.getWidth() - 
document.leftMargin() -
document.rightMargin());
//                      addressTable.writeSelectedRows(0, -1, 
document.leftMargin(),
page.getHeight() - document.topMargin() + addressTable.getTotalHeight() -
logoTable.getTotalHeight(), writer.getDirectContent());
                        cell = new PdfPCell(addressTable);
                        cell.setBorder(Rectangle.NO_BORDER);
                        headerTable.addCell(cell);
                        float[] billShipwidths = {0.35f,0.30f,0.35f};
                        PdfPTable billShipTable = new 
PdfPTable(billShipwidths);   

                        String billAddress = "";
                        /*if(invoiceViewBean.getCustomer1()!=null &&
invoiceViewBean.getCustomer1().length()>0)
                                billAddress=invoiceViewBean.getCustomer1()+" ";
                        if(invoiceViewBean.getCustomer2()!=null &&
invoiceViewBean.getCustomer2().length()>0)
                                
billAddress=billAddress+invoiceViewBean.getCustomer2();*/
                        billAddress=billAddress+invoiceViewBean.getBillToName();
                        //if(billAddress.length()==0)
                        billAddress=billAddress+"\n";
                        if(invoiceViewBean.getStreetB()!=null &&
invoiceViewBean.getStreetB().length()>0)
                                
billAddress=billAddress+invoiceViewBean.getStreetB()+"\n";
                        else
                                billAddress=billAddress+"\n";
                        if(invoiceViewBean.getCityB()!=null &&
invoiceViewBean.getCityB().length()>0)
                                
billAddress=billAddress+invoiceViewBean.getCityB()+" ";
                        if(invoiceViewBean.getStateB()!=null &&
invoiceViewBean.getStateB().length()>0)
                                
billAddress=billAddress+invoiceViewBean.getStateB()+" ";
                        if(invoiceViewBean.getPostalCodeB()!=null &&
invoiceViewBean.getPostalCodeB().length()>0)
                                
billAddress=billAddress+invoiceViewBean.getPostalCodeB();
                        if(!(invoiceViewBean.getCityB()!=null &&
invoiceViewBean.getCityB().length()>0)||!(invoiceViewBean.getStateB()!=null
&&
invoiceViewBean.getStateB().length()>0)||!(invoiceViewBean.getPostalCodeB()!=null
&& invoiceViewBean.getPostalCodeB().length()>0))
                                billAddress=billAddress+"\n";

        
                        String shipToName="";
                        if(invoiceViewBean.getCustomer1()!=null &&
invoiceViewBean.getCustomer1().length()>0)
                                shipToName=invoiceViewBean.getCustomer1()+" ";
                        if(invoiceViewBean.getCustomer2()!=null &&
invoiceViewBean.getCustomer2().length()>0)
                                
shipToName=shipToName+invoiceViewBean.getCustomer2();
                        invoiceViewBean.setShipToName(shipToName);

                        PdfPTable billTable = new PdfPTable(2);
                        cell = new PdfPCell(new Paragraph("BILL TO",font1));
//                      cell = new PdfPCell(new Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
//                      cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                        cell.setBorder(Rectangle.NO_BORDER);
                        billTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("
"+Integer.parseInt(invoiceViewBean.getBillTo()),font2));
//                      cell = new PdfPCell(new Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
//                      cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        billTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph(billAddress,font2));
//                      cell = new PdfPCell(new Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
//                      cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setColspan(2);
                        billTable.addCell(cell);

                        cell = new PdfPCell(billTable);
                        cell.setBorder(Rectangle.NO_BORDER);
                        billShipTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("    "));             
                        cell.setBorder(Rectangle.NO_BORDER);    
                        billShipTable.addCell(cell);    

                        PdfPTable shipTable = new PdfPTable(2);

                        if ((invoiceViewBean.getSoldTo() != null ||
!invoiceViewBean.getSoldTo().equals("")) && (invoiceViewBean.getShipTo() !=
null || !invoiceViewBean.getShipTo().equals("")))
                        {
                                if 
(invoiceViewBean.getSoldTo().equals(invoiceViewBean.getShipTo()))
                                {
                                        if(invoiceViewBean.getShipToName() == 
null ||
invoiceViewBean.getShipToName().equals(""))
                                        {
                                                
invoiceViewBean.setShipToName("---");
                                        }
                                        //cell = new PdfPCell(new 
Paragraph("SOLD TO"+"/"+"SHIP TO"+"    
"+Integer.parseInt(invoiceViewBean.getShipTo())+"\n"+invoiceViewBean.getShipToName()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getState()+" "+invoiceViewBean.getPostalCode(),font2));
                                        cell = new PdfPCell(new Paragraph("SOLD 
TO"+"/"+"SHIP TO",font1));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(new Paragraph(" 
"+Integer.parseInt(invoiceViewBean.getShipTo()),font2));
                                        
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(new
Paragraph(invoiceViewBean.getShipToName()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getState()+" "+invoiceViewBean.getPostalCode(),font2));
                                        //cell = new PdfPCell(new 
Paragraph("BILL TO"+"    
"+Integer.parseInt(invoiceViewBean.getBillTo())+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreetB()+"\n"+invoiceViewBean.getCityB()+"
"+invoiceViewBean.getStateB()+" "+invoiceViewBean.getPostalCodeB(),font2));
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("BILLTO", mm)+"    
"+invoiceViewBean.getBillTo()+"\n"+invoiceViewBean.getCustomer1()+"
"+invoiceViewBean.getCustomer2()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getPostalCode()+"\n"+invoiceViewBean.getState()+"
"+invoiceViewBean.getCountry()));               
                                
System.out.println(">>>>"+invoiceViewBean.getShipToName()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getState()+" "+invoiceViewBean.getPostalCode());
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        cell.setColspan(2);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(shipTable); 
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SOLDTO",
mm)+"/"+kk.getLiteral("SHIPTO", mm)+"    
"+invoiceViewBean.getShipTo()+"\n"+invoiceViewBean.getShipToName()));
                                }
                                else
                                {
                                        if(invoiceViewBean.getShipToName() == 
null ||
invoiceViewBean.getShipToName().equals(""))
                                        {
                                                
invoiceViewBean.setShipToName("---");
                                        }
                                        //cell = new PdfPCell(new 
Paragraph("SHIP TO"+"    
"+Integer.parseInt(invoiceViewBean.getShipTo())+"\n"+invoiceViewBean.getShipToName()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getState()+" "+invoiceViewBean.getPostalCode(),font2));
                                        cell = new PdfPCell(new Paragraph("SHIP 
TO",font1));
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(new Paragraph(" 
"+Integer.parseInt(invoiceViewBean.getShipTo()),font2));
                                        
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(new
Paragraph(invoiceViewBean.getShipToName()+"\n"+invoiceViewBean.getStreet1()+"\n"+invoiceViewBean.getCity()+"
"+invoiceViewBean.getState()+" "+invoiceViewBean.getPostalCode(),font2));
                                        cell.setColspan(2);
                                        cell.setBorder(Rectangle.NO_BORDER);
                                        shipTable.addCell(cell);
                                        cell = new PdfPCell(shipTable);         
                        

                                }
                        }

                        cell.setBorder(Rectangle.NO_BORDER);             
//                      cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        billShipTable.addCell(cell);

                        billShipTable.setSpacingBefore(25f);
                        billShipTable.setWidthPercentage(100);  
//                      document.add(billShipTable);
//                      billShipTable.setTotalWidth(page.getWidth() - 
document.leftMargin() -
document.rightMargin());
//                      billShipTable.writeSelectedRows(0, -1, 
document.leftMargin(),
page.getHeight() - document.topMargin() + billShipTable.getTotalHeight() -
addressTable.getTotalHeight() - logoTable.getTotalHeight(),
writer.getDirectContent());
                        cell = new PdfPCell(billShipTable);
                        cell.setBorder(Rectangle.NO_BORDER);
                        headerTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("\n"));
                        cell.setBorder(Rectangle.NO_BORDER);
                        headerTable.addCell(cell);
//                      headerTable.getDefaultCell().setBorderWidth(0f);
                        headerTable.setTotalWidth(page.getWidth() - 
document.leftMargin() -
document.rightMargin());
                        headerTable.writeSelectedRows(0, -1, 
document.leftMargin(),
page.getHeight() - document.topMargin() + headerTable.getTotalHeight(),
writer.getDirectContent());

                        headerHeight = billShipTable.getTotalHeight() +
addressTable.getTotalHeight() + logoTable.getTotalHeight();


                }
                catch (Exception e) {
                        throw new ExceptionConverter(e);
                }
        }

        public void generateInvoicePdf(HttpServletRequest
request,HttpServletResponse response,List invoices,boolean viewPriceFlag)
throws IOException, ApplicationException{

                double totalQuantity=0;
                double totalWeightShipped=0;
                double subTotal=0;
                //String url = null;

                //Initailizing invoiceList with original invoice list
                invoiceList = invoices;
                //Generating the pdf for Invoice
                NumberFormat nf = NumberFormat.getInstance(); 
                nf.setMinimumFractionDigits(2);
                nf.setMaximumFractionDigits(2);
                nf.setGroupingUsed(false);

                Font font1,font2,font3;

                font1 = new Font(Font.HELVETICA,8, Font.BOLD);
                font2 = new Font(Font.HELVETICA,8, Font.NORMAL);
                font3 = new Font(Font.HELVETICA,10,Font.BOLD);

                Document document = new Document(PageSize.A4, 36, 36, 
headerHeight, 36);
                document.setMargins(36, 36, 186, 36);           

                try{

                        String getCurrentUrl = 
request.getRequestURL().toString();
                        getCurrentUrl =
getCurrentUrl.substring(0,getCurrentUrl.lastIndexOf("/"));               
                        String urlPathForImage = getCurrentUrl 
+"/images/logo.jpg";

                        response.setContentType("application/pdf");
                        PdfWriter writer = PdfWriter.getInstance(document,
response.getOutputStream());
                        writer.setPageEvent(new 
InvoicePdfGenerator(invoices,urlPathForImage));

                        writer.setMargins(36, 36, headerHeight, 36);
//                      writer.setPageEvent(new );
                        document.open();  

                        Iterator it = invoices.iterator();
                        InvoiceViewBean invoiceViewBean = (InvoiceViewBean) 
it.next();
                        // Image image = Image.getInstance(new
ClassPathResource("images/logo.gif").getURL());         

                        //  order table 
                        float[] orderwidths = {0.28f, 0.21f, 0.30f, 0.21f};
                        PdfPTable orderTable = new PdfPTable(1);
                        //  order table row1
                        PdfPTable orderTableOne = new PdfPTable(orderwidths);
                        PdfPCell cell = null;


                        //If Credit Memo then it should come as Order/RMA Date
                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("G2"))
                        {
                                cell = new PdfPCell((new Paragraph("ORDER RMA 
DATE" , font1)));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("ORDERRMADATE", mm)));
                        }
                        else
                        {
                                cell = new PdfPCell(new Paragraph("ORDER 
DATE",font1));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("ORDERDATE", mm)));
                        }
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableOne.addCell(cell);

                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("G2"))
                        {
                                cell = new PdfPCell(new Paragraph("ORDER RMA 
NUMBER",font1));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("ORDERRMANUMBER",
mm)));
                        }
                        else
                        {
                                cell = new PdfPCell(new Paragraph("ORDER 
NUMBER",font1));
                                //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("ORDERNUMBER", mm)));
                        }
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableOne.addCell(cell);

                        cell = new PdfPCell(new Paragraph("PURCHASE 
ORDER",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("PURCHASEORDER", mm)));               
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableOne.addCell(cell);

                        cell = new PdfPCell(new Paragraph("STORE 
NUMBER",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("STORENUMBER", mm)));         
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableOne.addCell(cell);                    

                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getOrderDate(),font2));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableOne.addCell(cell);
                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getOrderNumber(),font2));          
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableOne.addCell(cell);
                        if(invoiceViewBean.getPoNumber() == null ||
invoiceViewBean.getPoNumber().equals(""))
                        {
                                invoiceViewBean.setPoNumber("---");
                        }
                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getPoNumber(),font2));             
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableOne.addCell(cell);

                        if(invoiceViewBean.getStoreID() == null ||
invoiceViewBean.getStoreID().equals(""))
                        {
                                invoiceViewBean.setStoreID("---");
                        }
                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getStoreID(),font2));              
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableOne.addCell(cell);
                        orderTable.addCell(orderTableOne);               

                        //order table row2
                        PdfPTable orderTableTwo = new PdfPTable(orderwidths);

                        cell = new PdfPCell(new Paragraph("SHIP METHOD",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SHIPMETHOD", mm)));          
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableTwo.addCell(cell);

                        cell = new PdfPCell(new Paragraph("SHIPMENT 
TRACKING",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SHIPTRACK", mm)));           
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableTwo.addCell(cell);

                        cell = new PdfPCell(new Paragraph("TERMS OF 
PAYMENT",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("TERMSOFPAYMENT",
mm)));          
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableTwo.addCell(cell);      

                        cell = new PdfPCell(new Paragraph("PAY DUE 
DATE",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("PAYDUEDATE", mm)));          
                        cell.setBorder(Rectangle.NO_BORDER);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        orderTableTwo.addCell(cell);                     

                        //order table row4

                        //If Credit Memo ShipmentMethod and tracking number 
should be empty
                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("G2"))
                        {
                                cell = new PdfPCell(new Paragraph(""));
                        }
                        else
                        {
                                if(invoiceViewBean.getShipmentMethod() == null 
||
invoiceViewBean.getShipmentMethod().equals(""))
                                {
                                        
invoiceViewBean.setShipmentMethod("---");
                                }
                                cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getShipmentMethod(),font2));
                        }
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableTwo.addCell(cell);

                        if 
(invoiceViewBean.getDocumentType().equalsIgnoreCase("G2"))
                        {
                                cell = new PdfPCell(new Paragraph(""));
                        }
                        else
                        {
                                if(invoiceViewBean.getShipmentTracking() == 
null ||
invoiceViewBean.getShipmentTracking().equals(""))
                                {
                                        
invoiceViewBean.setShipmentTracking("---");
                                }
                                cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getShipmentTracking(),font2));      
                        }
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableTwo.addCell(cell);
                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getPaymentTerms(),font2)); 
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableTwo.addCell(cell);
                        cell = new PdfPCell(new
Paragraph(""+invoiceViewBean.getDueDate(),font2));      
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBorder(Rectangle.NO_BORDER);
                        orderTableTwo.addCell(cell);
                        orderTable.addCell(orderTableTwo);

                        orderTable.setSpacingBefore(25f);
                        orderTable.setWidthPercentage(100);
                        orderTable.setHorizontalAlignment(Element.ALIGN_LEFT);  
   

                        document.add(orderTable);

                        //customer Notes
                        PdfPTable customerNotesTable = new PdfPTable(1);
                        //customerNotesTable row1

                        cell = new PdfPCell(new Paragraph("CUSTOMER 
NOTES",font3));
                                
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        customerNotesTable.addCell(cell);
                        //customerNotesTable row2
                        if (invoiceViewBean.getCustomerText() != null)
                        {
                                
                                String []custText = 
invoiceViewBean.getCustomerText().split(":");
                                
                                if (custText.length == 3)
                                        cell = new PdfPCell(new
Paragraph(custText[0]+"\n"+custText[1]+"\n"+custText[2],font2));
                                if (custText.length == 2)
                                        cell = new PdfPCell(new 
Paragraph(custText[0]+"\n"+custText[1],font2));
                                if (custText.length == 1)
                                        cell = new PdfPCell(new 
Paragraph(custText[0],font2));
                                //cell.setBorder(Rectangle.NO_BORDER);
                                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                //addressTable.addCell(cell);
                        }
                        else
                                cell = new PdfPCell(new Paragraph("  "));

                        //cell = new PdfPCell(new
Paragraph(invoiceViewBean.getCustomerText(),font2));
                        customerNotesTable.addCell(cell);

                        customerNotesTable.setSpacingBefore(25f);
                        customerNotesTable.setWidthPercentage(100);
                        
customerNotesTable.setHorizontalAlignment(Element.ALIGN_LEFT);    
                        document.add(customerNotesTable);

                        //InvoiceDetails table
                        float[] invoicewidths = {0.15f, 0.5f, 0.13f, 0.22f};
                        PdfPTable invoiceTable = new PdfPTable(invoicewidths);
                        //invoiceTable row1

                        cell = new PdfPCell(new Paragraph("INVOICE 
DETAILS",font3));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("INVOICEDETAILS",
mm)));
                        cell.setColspan(4);
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceTable.addCell(cell);
                        //invoiceTable row2

                        cell = new PdfPCell(new Paragraph("QUANTITY 
SHIPPED",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("QUANTITYSHIPPED",
mm)));  
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("PRODUCT 
DESCRIPTION",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("PRODUCTDESC", mm)));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("UNIT PRICE",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("UNITPRICE", mm)));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceTable.addCell(cell);

                        cell = new PdfPCell(new Paragraph("EXTENDED PRICE"+"
("+invoiceViewBean.getCurrencyCode().toUpperCase()+")",font1 ));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("EXTENDEDPRICE", mm)+"
("+invoiceViewBean.getCurrencyCode().toUpperCase()+")" ));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceTable.addCell(cell);

                        PdfPTable invoiceDesc = new PdfPTable(1);
                        PdfPTable invoicePrice = new PdfPTable(1);
                        PdfPTable invoiceExtn = new PdfPTable(1);       

                        //Invoice Table row3
                        List invoiceLineList = 
invoiceViewBean.getInvoiceLineList();
                        InvoiceLine invoiceLine = new InvoiceLine();
                        String taxDesc="";
                        String taxAmount="";
                        String freightCharges="";

                        if(invoiceLine.getPatientName()==null)
                        {
                                //cell = new PdfPCell(new
Paragraph("\n\n"+invoiceViewBean.getFreightCharges(),font2));   
                                
freightCharges="\n"+dollarFormat(invoiceViewBean.getFreightCharges());
                        }
                        else
                        {
                                //cell = new PdfPCell(new
Paragraph("\n\n\n"+invoiceViewBean.getFreightCharges(),font2));
                                
freightCharges="\n"+dollarFormat(invoiceViewBean.getFreightCharges());
                        }

                        //Code for printing taxes
                        List invoiceTaxList = 
invoiceViewBean.getInvoiceTaxList();
                        InvoiceTax invoiceTax = new InvoiceTax();
                        

                        for(int index=0;index<invoiceTaxList.size();index++)
                        {                     
                                invoiceTax = (InvoiceTax) 
invoiceTaxList.get(index);

                        
if(!invoiceTax.getTaxRate().equals("0.0")||invoiceTax.getTaxRate().equals("0")){


                                        
taxDesc=taxDesc+"\n"+invoiceTax.getTaxDesc()+" 
"+dollarFormat(Double.parseDouble(invoiceTax.getTaxRate()))+"%";

                                
taxAmount=taxAmount+"\n"+dollarFormat(Double.parseDouble(invoiceTax.getTaxAmount()));
                                }
                        }


                
                        for(int i=0;i<invoiceLineList.size();i++)
                        {                     
                                invoiceLine = (InvoiceLine) 
invoiceLineList.get(i);
                                
                                String patientName="";
                                if(invoiceLine.getPatientName()!=null)
                                        
patientName=invoiceLine.getPatientName();
                         
                                totalQuantity = 
totalQuantity+invoiceLine.getQuantity();
                                
                                subTotal = 
subTotal+invoiceLine.getExtendedPrice();

                                cell = new PdfPCell(new
Paragraph("\n\n"+(int)invoiceLine.getQuantity()+"
"+invoiceLine.getUnitOfMeasure(),font2));       
                                
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                cell.disableBorderSide(PdfPCell.BOTTOM);
                                cell.disableBorderSide(PdfPCell.TOP);
                                invoiceTable.addCell(cell);

                                cell = new PdfPCell(new
Paragraph("\n"+Integer.parseInt(invoiceLine.getMaterialNumber())+"\n"+invoiceLine.getMaterialDesc()+"\n"+patientName+"\n",font2));
      
                                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                cell.disableBorderSide(PdfPCell.BOTTOM);
                                cell.disableBorderSide(PdfPCell.TOP);
                                invoiceTable.addCell(cell);


                                if (viewPriceFlag == true)
                                {
                                        if (i == 0)
                                                cell = new PdfPCell(new
Paragraph("\n"+dollarFormat(invoiceLine.getUnitPrice()),font2));
                                        else
                                                cell = new PdfPCell(new
Paragraph("\n"+dollarFormat(invoiceLine.getUnitPrice()),font2));
                                }
                                else
                                {
                                        if (i == 0)
                                                cell = new PdfPCell(new 
Paragraph("\n ----",font2));
                                        else
                                                cell = new PdfPCell(new 
Paragraph("\n ----",font2));
                                }
                                
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                cell.disableBorderSide(PdfPCell.BOTTOM);
                                cell.disableBorderSide(PdfPCell.TOP);
                        
                                invoiceTable.addCell(cell);

                                String price="";
                                //Putting * for taxable item
                                if (viewPriceFlag == true)
                                {
                                        if 
(invoiceLine.getTaxableItem().equalsIgnoreCase("Y"))
                                        {
                                                //cell = new PdfPCell(new
Paragraph("\n"+nf.format(invoiceLine.getExtendedPrice())+"*",font2));
                                                
price="\n"+dollarFormat(invoiceLine.getExtendedPrice())+"*";
                                        }
                                        else
                                        {
                                                //cell = new PdfPCell(new
Paragraph("\n"+nf.format(invoiceLine.getExtendedPrice()),font2));
                                                
price="\n"+dollarFormat(invoiceLine.getExtendedPrice());
                                        }
                                }
                                else
                                {
                                        //cell = new PdfPCell(new Paragraph("\n 
----",font2));
                                        price="\n ----";
                                }
                                cell=new PdfPCell(new Paragraph(price,font2));
                                
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                cell.disableBorderSide(PdfPCell.BOTTOM);
                                cell.disableBorderSide(PdfPCell.TOP);
                                //cell.setBorder(Rectangle.NO_BORDER);
                                //invoiceExtn.addCell(cell);
                                invoiceTable.addCell(cell);

                                /*cell = new PdfPCell(new Paragraph("\n\n"));   
                                
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                cell.setBorder(Rectangle.NO_BORDER);
                                invoiceExtn.addCell(cell);*/
                        }       



                        cell=new PdfPCell(new Paragraph("\n")); 
                        cell.disableBorderSide(PdfPCell.BOTTOM);
                        cell.disableBorderSide(PdfPCell.TOP);
                        invoiceTable.addCell(cell);

                        cell=new PdfPCell(new Paragraph("\nShipping and
Handling"+taxDesc,font2));
                        cell.disableBorderSide(PdfPCell.BOTTOM);
                        cell.disableBorderSide(PdfPCell.TOP);
                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        invoiceTable.addCell(cell);

                        cell=new PdfPCell(new Paragraph("\n")); 
                        cell.disableBorderSide(PdfPCell.BOTTOM);
                        cell.disableBorderSide(PdfPCell.TOP);
                        invoiceTable.addCell(cell);

                        cell=new PdfPCell(new 
Paragraph(freightCharges+taxAmount,font2));                       
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        cell.disableBorderSide(PdfPCell.BOTTOM);
                        cell.disableBorderSide(PdfPCell.TOP);
                        invoiceTable.addCell(cell);

                        cell=new PdfPCell(new Paragraph("\n")); 
                        cell.disableBorderSide(PdfPCell.BOTTOM);
                        cell.disableBorderSide(PdfPCell.TOP);
                        invoiceTable.addCell(cell);

                        //Putting freight charges

//                      cell = new PdfPCell(new Paragraph("\nFREIGHT",font2));  
//                      cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                        //cell.setBorder(Rectangle.NO_BORDER);
                        //invoiceDesc.addCell(cell);
                        //invoiceTable.addCell(cell);
                        //cell.setBorder(Rectangle.NO_BORDER);

                        //invoiceExtn.addCell(cell);
                        //invoiceTable.addCell(cell);

                        //Code for printing taxes
                        //Iterator it2 = invoices.iterator();
                        /*                      List invoiceTaxList = 
invoiceViewBean.getInvoiceTaxList();
                        InvoiceTax invoiceTax = new InvoiceTax(); 
                        for(int i=0;i<invoiceTaxList.size();i++)
                        {                     
                                invoiceTax = (InvoiceTax) invoiceTaxList.get(i);

                                if(!invoiceTax.getTaxRate().equals("0.0")){
                                cell = new PdfPCell(new 
Paragraph("\n"+invoiceTax.getTaxDesc()+" 
"+invoiceTax.getTaxRate()+"%" ,font2 ));        
                                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                cell.setBorder(Rectangle.NO_BORDER);
                                invoiceDesc.addCell(cell);

                                cell = new PdfPCell(new
Paragraph("\n"+invoiceTax.getTaxAmount(),font2));       
                                
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                cell.setBorder(Rectangle.NO_BORDER);
                                invoiceExtn.addCell(cell);
                                }
                        }
                        invoiceTable.addCell(invoiceDesc);
                        invoiceTable.addCell(invoicePrice);
                        invoiceTable.addCell(invoiceExtn);
                         */
                        System.out.println("taxAmount   >> "+taxAmount);
                        System.out.println("taxDesc   >>"+taxDesc);
                        System.out.println("freightCharges  >>"+freightCharges);

                        invoiceTable.setSpacingBefore(25f);
                        invoiceTable.setWidthPercentage(100);

                        document.add(invoiceTable);

                        //INVOICE TABLE ROW4
                        float[] invoiceR4widths = {0.15f, 0.25f, 0.25f, 0.35f};
                        PdfPTable invoiceR4 = new PdfPTable(invoiceR4widths);   
         

                        cell = new PdfPCell(new Paragraph("TOTAL QTY 
SHIPPED",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("TOTALQTYSHIPPED",
mm)));
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceR4.addCell(cell);                     

                        cell = new PdfPCell(new Paragraph("SUBTOTAL",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("SUBTOTAL", mm)));
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceR4.addCell(cell);

                        cell = new PdfPCell(new Paragraph("TOTAL TAXES",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("TOTALTAXES", mm)));
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceR4.addCell(cell);

                        cell = new PdfPCell(new Paragraph("TOTAL AMOUNT 
DUE",font1));
                        //cell = new PdfPCell(new 
Paragraph(kk.getLiteral("TOTALAMOUNTDUE",
mm)));
                        cell.setGrayFill(0.75f);
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invoiceR4.addCell(cell);                                
     

                        //INVOICE TABLE ROW5
                        cell = new PdfPCell(new Paragraph((int)totalQuantity+"
"+invoiceLine.getUnitOfMeasure(),font2));       
                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invoiceR4.addCell(cell);
                        if (viewPriceFlag == true)
                                cell = new PdfPCell(new
Paragraph(dollarFormat(invoiceViewBean.getSubTotal()),font2));
                        else
                                cell = new PdfPCell(new 
Paragraph("----",font2));

                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceR4.addCell(cell);
                        if (viewPriceFlag == true)
                                cell = new PdfPCell(new
Paragraph(dollarFormat(invoiceViewBean.getTotalTaxAmount()),font2));
                        else
                                cell = new PdfPCell(new 
Paragraph("----",font2));

                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        invoiceR4.addCell(cell);

                        //double
totalAmountDue=(double)subTotal+invoiceViewBean.getTotalTaxAmount();
                        double
totalAmountDue=invoiceViewBean.getSubTotal()+invoiceViewBean.getTotalTaxAmount();

                        if (viewPriceFlag == true)
                                cell = new PdfPCell(new 
Paragraph(dollarFormat(totalAmountDue),font3));
                        else
                                cell = new PdfPCell(new 
Paragraph("----",font2));

                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                        invoiceR4.addCell(cell);                             
                        invoiceR4.setWidthPercentage(100);
                        invoiceR4.setHorizontalAlignment(Element.ALIGN_CENTER); 
        
                        document.add(invoiceR4);

                        if (invoiceViewBean.getFooter2() != null)
                        {
                                String []footerText = 
invoiceViewBean.getFooter2().split(":");
                                if (footerText.length == 3)
                                        document.add(new
Paragraph(invoiceViewBean.getFooter1()+"\n"+footerText[0]+"
"+footerText[1]+" "+footerText[2],font2));
                                if (footerText.length == 2)
                                        document.add(new
Paragraph(invoiceViewBean.getFooter1()+"\n"+footerText[0]+"
"+footerText[1],font2));
                                if (footerText.length == 1)
                                        document.add(new
Paragraph(invoiceViewBean.getFooter1()+"\n"+footerText[0],font2));
                        }
                        else
                        {
                                document.add(new 
Paragraph(invoiceViewBean.getFooter1(),font2));
                        }

                        document.getPageNumber();       
                        document.close(); 

                }catch(DocumentException de)
                {
                        logger.error(de.getMessage(), de);
                        throw new ApplicationException(de.getMessage(),de);
                }
        }
        
        private String dollarFormat(double value){
                NumberFormat nf = NumberFormat.getInstance();   
                nf.setMaximumFractionDigits(2);
                nf.setMinimumFractionDigits(2);
                nf.setGroupingUsed(false);      
                MomentumLocale momentumLocale = new MomentumLocale();
        
                momentumLocale.setCountry("US");
                momentumLocale.setLanguage("en");
                //}
                String localizedValue="";       
                String localizedValue2="";
                String value2=nf.format(value);
                double value3=Double.valueOf(value2).doubleValue();
                Number numberObj = new Double(value3);
                try{
                        I18nServiceProvider i18nServiceProvider = new 
I18nServiceProvider();
                        localizedValue =
i18nServiceProvider.getLocalizedNumber(numberObj,momentumLocale);
                        
                        int dotIndex = localizedValue.indexOf('.');
                        int valueLength =
localizedValue.substring(dotIndex+1,localizedValue.length()).length();
                        if(localizedValue.equals("0")){
                                localizedValue2="0.00";
                        }
                        else{
                                if(valueLength==1)
                                {
                                        localizedValue = localizedValue + "0";
                                }                       
                                 if(dotIndex==-1)
                                {
                                        localizedValue = localizedValue + ".00";
                                }       
                                 localizedValue2=localizedValue;
                        }
                        
                        if(localizedValue2.equals("00.00")){
                                localizedValue2="0.00";
                        }
                }catch(Exception e){
                        
                }       
                
                return localizedValue2; 
        }

}






-- 
View this message in context: 
http://www.nabble.com/Page-Number-not-getting-diaplayed-in-Adobe-Reader-tp17595125p17595125.html
Sent from the iText - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Do you like iText?
Buy the iText book: http://www.1t3xt.com/docs/book.php
Or leave a tip: https://tipit.to/itexttipjar

Reply via email to