Hi there,
I have consecutive dynamic data to display through Itext pdf, however, if I use the CelFitPages, there will be a small extra empty row to be displayed at the bottom. Because the data structure I used forced me to use lots of loops, therefore, it's hard to do table.deleteLastRow and call other methods to run the incompleted rows on the next page.
Please advise. Any suggestions will be greatly appreciated.
Thanks a lot.
Code attached.


package com.empire.pdf.billing;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import com.empire.paw.model.report.billing.*;
import com.empire.pdf.util.ReportException;
import com.empire.pdf.util.TableStyleSheet;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

/**
* @author Arinas Lee 2-Oct-03
*/
public class ContractCurrentBillingReport {
private static Font font12 = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
private static Font font12B = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
private static Font font14B = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD);
private static Font font14 = FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL);
private String[] labelCerNo = { "Certificate Number", "Num�ro du certificat" };
private String[] labelName = { "Name", "Nom" };
private String[] labelLife = { "Life", "Vie" };
private String[] labelAdnd = { "AD&D", "D�c�s et mut. acc." };
private String[] labelWi = { "WI", "Indemnit�s hebd." };
private String[] labelLtd = { "LTD", "Inv. longue dur�e" };
private String[] labelEhb = { "EHB", "AMC" };
private String[] labelDent = { "Dent", "Ass. dent" };
private String[] labelTax = { "Tax", "Taxe" };
private String[] labelTotal = { "Total", "Total" };
private String[] labelVolume = { "Volume", "Volume" };
private String[] labelPremium = { "Premium", "Prime" };
private String[] labelDepartmentTotals = { "Department Totals: ", "Total du departement: " };
private String[] labelDivTotals = { "Division Totals: ", "Total de la division: " };
private String[] labelFooter = {"Data on this report reflects volumes and premiums as billed by Empire Life on ", "Les donn�es figurant dans ce relev� correspondent aux volumes et primes factur�s par l�Empire Vie au " };


private String locale;
private BillingReport billingInfo;
private HeaderReport header;
TableStyleSheet ts = new TableStyleSheet();
DecimalFormat df= ts.decimalNumber();
private final float[] titleWidths = { 47, 120, 55, 55, 45, 45, 40, 35, 35, 45, 50, 65, 50, 65 };
/**
* Constructor of ContractCurrentBillingReport
*/
public ContractCurrentBillingReport(BillingReport billingInfo, String loc) {
this.billingInfo = billingInfo;
this.locale = loc;
}


        /**
         * Method createBillingReport.
         * @return byte[]
         * @throws ReportException
         */
        public byte[] createBillingReport() throws ReportException {
                ByteArrayOutputStream content = new ByteArrayOutputStream();
                int loc = locale.equalsIgnoreCase("en_CA") ? 0 : 1;
                long startT = System.currentTimeMillis();
                try {
                        Document document = new Document(PageSize.LEDGER, 10, 10, 20, 
20);
                //      Display header information on all pages.
                        createBillingHeader(document, loc,content);
                //      Display footer information on all pages.
                        createBillingFooter(document, billingInfo.getHeader(), loc);
                //      Display repeating department blocks.
                        createBillingDepartments(document, loc, 
billingInfo.getDepartments());
                        document.close();
                } catch (ReportException re) {
                        throw re;
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new ReportException(e);
                }
                long endT = System.currentTimeMillis();
                System.out.println("Generation time: " + Long.toString(endT - startT));
                return content.toByteArray();
        }

/**
* Method createBillingHeader.
* @param document
* @param loc
*/
private ByteArrayOutputStream createBillingHeader(Document document, int loc, ByteArrayOutputStream content) {
try{
PdfWriter writer =
PdfWriter.getInstance(document,new FileOutputStream("C:\\Arinas\\BillingReport.pdf"));
// PdfWriter.getInstance(document, content);
ContractBillingHeader events = new ContractBillingHeader(billingInfo, loc);
writer.setPageEvent(events);
}
catch(Exception e){
e.printStackTrace();
}
return content;
}


/**
* Method createBillingFooter.
* @param head
* @param loc
* @return HeaderFooter
*/
private HeaderFooter createBillingFooter(Document document, HeaderReport head, int loc) {
HeaderFooter footer = null;
try{
String billDate = ts.tranlateHeaderFooterDate(loc,head.getBillDate());
footer = new HeaderFooter(new Phrase(labelFooter[loc] + billDate + " page: "), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
footer.setBorder(Rectangle.BOX);
document.setFooter(footer);
}
catch(Exception e) {
e.printStackTrace();
}
return footer;
}


/**
* Method createBillingDepartments.
* @param document
* @param loc
* @param departments
* @param table
* @throws ReportException
*/
private void createBillingDepartments(Document document, int loc, List departments) throws ReportException {
document.open();
Table table = null;
try {
ContractBillingCalculator divCalc = new ContractBillingCalculator();
HashMap resultCal = new HashMap();
resultCal = divCalc.addDivisionSummary(billingInfo);
ArrayList depRep = (ArrayList) resultCal.get("Dept");
double[] divSum = (double[]) resultCal.get("divResult");
// for (int i = 0; i < departments.size(); i++) {
for (int i = 0; i < depRep.size(); i++) {
DepartmentReport departmentInfo = (DepartmentReport) depRep.get(i);
String DeptName = "";
DeptName = departmentInfo.getDepartmentName();
// Drawing the table to hold all static table and dynamic data.
table = ts.createTable(titleWidths,14,10);
createDepartmentHeader(table,loc, DeptName);
createBillingCertificates(departmentInfo.getCertificates(),table);
if(departmentInfo.isOrderFlag() == true) {
createDepartmentSummary(departmentInfo, table, loc);
}
document.add(table);
}
table.addCell(ts.createRegularCell("",font12,1,14,0));
createDivisionSummary(document, divSum, loc,table);
} catch (ReportException re) {
throw re;
} catch (Exception e) {
e.printStackTrace();
throw new ReportException(e);
}
}


/**
* Method createDepartmentHeader.
* @param table
* @param loc
* @param deptName
*/
private Table createDepartmentHeader(Table table, int loc, String deptName) {
try {
table.addCell(ts.getDeptNameCell(deptName, font14B, 1, 14,0));
table.addCell(ts.getDeptTitleCell(labelCerNo[loc], font14B, 2,1,1));
table.addCell(ts.getDeptTitleCell(labelName[loc], font14B,2,1,1));
table.addCell(ts.getDeptTitleCell(labelLife[loc], font14B,1,2,1));
table.addCell(ts.getDeptTitleCell(labelAdnd[loc], font14B,1,2,1));
table.addCell(ts.getDeptTitleCell(labelWi[loc], font14B,1,2,1));
table.addCell(ts.getDeptTitleCell(labelLtd[loc], font14B,1,2,1));
table.addCell(ts.getDeptTitleCell(labelEhb[loc], font14B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelDent[loc], font14B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelTax[loc], font14B,2,1,1));
table.addCell(ts.getDeptTitleCell(labelTotal[loc], font14B,2,1,1));
table.addCell(ts.getDeptTitleCell(labelVolume[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelVolume[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelVolume[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12,1,1,1));
table.addCell(ts.getDeptTitleCell(labelVolume[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12B,1,1,1));
table.addCell(ts.getDeptTitleCell(labelPremium[loc], font12B,1,1,1));
table.endHeaders();
}catch(Exception e) {
e.printStackTrace();
}
return table;
}


        /**
         * Method createBillingCertificates.
         * @param details
         * @param table
         * @throws ReportException
         */

private void createBillingCertificates(List details, Table table) throws ReportException {

for (int i = 0; i < details.size(); i++) {
try {
CertificateReport billingContentInfo = (CertificateReport) details.get(i);
table.addCell(billingContentInfo.getCertificateNumber());
HashMap deptContent = createBillingCertificateContents(billingContentInfo.getCertificateDetails());
StringBuffer name = (StringBuffer) deptContent.get("name");
StringBuffer lifeV = (StringBuffer) deptContent.get("lifeV");
StringBuffer lifeP = (StringBuffer) deptContent.get("lifeP");
StringBuffer adndV = (StringBuffer) deptContent.get("adndV");
StringBuffer adndP = (StringBuffer) deptContent.get("adndP");
StringBuffer wiV = (StringBuffer) deptContent.get("wiV");
StringBuffer wiP = (StringBuffer) deptContent.get("wiP");
StringBuffer ltdV = (StringBuffer) deptContent.get("ltdV");
StringBuffer ltdP = (StringBuffer) deptContent.get("ltdP");
StringBuffer ehb = (StringBuffer) deptContent.get("ehb");
StringBuffer dent = (StringBuffer) deptContent.get("dent");
StringBuffer tax = (StringBuffer) deptContent.get("tax");
StringBuffer total = (StringBuffer) deptContent.get("total");
table.addCell(new Phrase(name.toString(), font12));
table.addCell(ts.getCertCell(lifeV.toString(), font12));
table.addCell(ts.getCertCell(lifeP.toString(), font12));
table.addCell(ts.getCertCell(adndV.toString(), font12));
table.addCell(ts.getCertCell(adndP.toString(), font12));
table.addCell(ts.getCertCell(wiV.toString(), font12));
table.addCell(ts.getCertCell(wiP.toString(), font12));
table.addCell(ts.getCertCell(ltdV.toString(), font12));
table.addCell(ts.getCertCell(ltdP.toString(), font12));
table.addCell(ts.getCertCell(ehb.toString(), font12));
table.addCell(ts.getCertCell(dent.toString(), font12));
table.addCell(ts.getCertCell(tax.toString(), font12));
table.addCell(ts.getCertCell(total.toString(), font12));
} catch (ReportException re) {
throw re;
} catch (Exception e) {
e.printStackTrace();
throw new ReportException(e);
}
}
}


/**
* Method createBillingCertificateContents.
* @param details
* @return HashMap
* @throws ReportException
*/
private HashMap createBillingCertificateContents(List details) throws ReportException {
StringBuffer nameBf = new StringBuffer();
StringBuffer lifeVBf = new StringBuffer();
StringBuffer lifePBf = new StringBuffer();
StringBuffer adndVBf = new StringBuffer();
StringBuffer adndPBf = new StringBuffer();
StringBuffer wiVBf = new StringBuffer();
StringBuffer wiPBf = new StringBuffer();
StringBuffer ltdVBf = new StringBuffer();
StringBuffer ltdPBf = new StringBuffer();
StringBuffer ehbCertBf = new StringBuffer();
StringBuffer dentCertBf = new StringBuffer();
StringBuffer taxCertBf = new StringBuffer();
StringBuffer totalCertBf = new StringBuffer();
String lifePremStr = "";
String adndPremStr = "";
String wiPremStr = "";
String ltdPremStr = "";
String ehbPremStr = "";
String dentPremStr = "";
String taxStr = "";
String totalStr = "";
HashMap certContent = new HashMap();
double lifePremDbl = 0;
double adndPremDbl = 0;
double wiPremDbl = 0;
double ltdPremDbl = 0;
double ehbPremDbl = 0;
double dentPremDbl = 0;
double taxDbl = 0;
double totalDbl = 0;


try {
int size = details.size();
for (int i = 0; i < size; i++) {
try {
CertificateDetailReport certificateContentInfo = (CertificateDetailReport) details.get(i);


if (certificateContentInfo.getLifePremium() == "")
lifePremStr = "";
else {
lifePremDbl = Double.parseDouble(certificateContentInfo.getLifePremium());
lifePremStr = ts.checkMinusValue(df.format(lifePremDbl));
}
if (certificateContentInfo.getAdndPremium() == "")
adndPremStr = "";
else {
adndPremDbl = Double.parseDouble(certificateContentInfo.getAdndPremium());
adndPremStr = ts.checkMinusValue(df.format(adndPremDbl));
}
if (certificateContentInfo.getWiPremium() == "")
wiPremStr = "";
else{
wiPremDbl = Double.parseDouble(certificateContentInfo.getWiPremium());
wiPremStr = ts.checkMinusValue(df.format(wiPremDbl));
}
if (certificateContentInfo.getLtdPremium() == "")
ltdPremStr = "";
else {
ltdPremDbl = Double.parseDouble(certificateContentInfo.getLtdPremium());
ltdPremStr = ts.checkMinusValue(df.format(ltdPremDbl));
}
if (certificateContentInfo.getEhbPremium() == "")
ehbPremStr = "";
else {
ehbPremDbl = Double.parseDouble(certificateContentInfo.getEhbPremium());
ehbPremStr = ts.checkMinusValue(df.format(ehbPremDbl));
}
if (certificateContentInfo.getDentPremium() == "")
dentPremStr = "";
else {
dentPremDbl = Double.parseDouble(certificateContentInfo.getDentPremium());
dentPremStr = ts.checkMinusValue(df.format(dentPremDbl));
}
if (certificateContentInfo.getTax() == "")
taxStr= "";
else {
taxDbl = Double.parseDouble(certificateContentInfo.getTax());
taxStr = ts.checkMinusValue(df.format(taxDbl));
}
if (certificateContentInfo.getTotal() == "")
totalStr = "";
else {
totalDbl = Double.parseDouble(certificateContentInfo.getTotal());
totalStr = ts.checkMinusValue(df.format(totalDbl));
}
nameBf.append(certificateContentInfo.getBilledName() + "\n");
lifeVBf.append(ts.checkMinusValue(certificateContentInfo.getLifeVolume()) + "\n");
lifePBf.append(lifePremStr +"\n");
adndVBf.append(ts.checkMinusValue(certificateContentInfo.getAdndVolume()) + "\n");
adndPBf.append(adndPremStr +"\n");
wiVBf.append(ts.checkMinusValue(certificateContentInfo.getWiVolume()) + "\n");
wiPBf.append(wiPremStr + "\n");
ltdVBf.append(ts.checkMinusValue(certificateContentInfo.getLtdVolume()) + "\n");
ltdPBf.append(ltdPremStr + "\n");
ehbCertBf.append(ehbPremStr + "\n");
dentCertBf.append(dentPremStr + "\n");
taxCertBf.append(taxStr + "\n");
totalCertBf.append(totalStr +"\n");
} catch (NullPointerException ne) {
ne.printStackTrace();
}
certContent.put("name",nameBf);
certContent.put("lifeV", lifeVBf);
certContent.put("lifeP", lifePBf);
certContent.put("adndV", adndVBf);
certContent.put("adndP", adndPBf);
certContent.put("wiV", wiVBf);
certContent.put("wiP", wiPBf);
certContent.put("ltdV", ltdVBf);
certContent.put("ltdP", ltdPBf);
certContent.put("ehb", ehbCertBf);
certContent.put("dent", dentCertBf);
certContent.put("tax", taxCertBf);
certContent.put("total", totalCertBf);
}
} catch (Exception e) {
e.printStackTrace();
throw new ReportException(e);
}
return certContent;
}


/**
* Method createDepartmentSummary.
* @param dept
* @param table
* @param loc
* @return Table
*/
private Table createDepartmentSummary(DepartmentReport dept, Table table, int loc){
try{
Phrase p =new Phrase(labelDepartmentTotals[loc] + dept.getDepartmentName(), font12);
Cell totalNameCell = new Cell();
totalNameCell.add(p);
totalNameCell.setColspan(2);
table.addCell(totalNameCell);
table.addCell(ts.getCertCell("",font12));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptLifePremium())), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptAdndPremium())), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptWiPremium())), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptLtdPremium())), font12));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptEhbPremium())), font12));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptDentPremium())), font12));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptTax())), font12));
table.addCell(ts.getCertCell(ts.checkDeptMinusValue(df.format(dept.getDeptTotal())), font12));
}
catch(Exception e) {
e.printStackTrace();
}
return table;
}


/**
* Method createDivisionSummary.
* @param document
* @param divSummmary
* @param loc
*/
private void createDivisionSummary(Document document, double[] divSummmary, int loc, Table table) {
try{
table = ts.createTable(titleWidths,14,5);
Phrase p4 = new Phrase(labelDivTotals[loc], font14);
Cell totalNameCell = new Cell();
totalNameCell.add(p4);
totalNameCell.setColspan(2);
totalNameCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(totalNameCell);
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[0]))).toString(), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[1]))).toString(), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[2]))).toString(), font12));
table.addCell(new Phrase("", font14));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[3]))).toString(), font12));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[4]))).toString(), font12));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[5]))).toString(), font12));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[6]))).toString(), font12));
table.addCell(ts.getCertCell(ts.checkMinusValue(df.format(new Double(divSummmary[7]))).toString(), font12));
document.add(table);
}catch(Exception e) {
e.printStackTrace();
}
}
}


_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=dept/bcomm&pgmarket=en-ca&RU=http%3a%2f%2fjoin.msn.com%2f%3fpage%3dmisc%2fspecialoffers%26pgmarket%3den-ca




-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to