First, I just want to say that iText is a great tool; however, I'm sure that's obvious by now
Second, I've created the following document here:
http://members.cox.net/remodel.johnson/test.pdf
Each page is made up of a table with two cells. The left cell contains a multi-row and multi-column table. The right cell contains a single cell table with an image in it.
The table on the first page renders correctly. However, after adding a new page, subsequent tables duplicate the first line of the table (the header line). I'd appreciate any help you can give me to figure out why this happens.
The relevant code is as follows:
private static final String[] strArColumnHeaders = {"Session ID", "Day", "Trial no.", "Samples", "Hits", "Misses", "Miss Rate"};
private static PdfWriter writer;
public static void ExportReportToPDFFile(String strSaveFullFileName, ArrayList alCharts,
ArrayList alChartData, String strTableType){
Document docMyDocument = new Document (PageSize.LETTER.rotate(), 25, 25, 36, 36);
try {
writer = PdfWriter.getInstance(docMyDocument, new FileOutputStream(strSaveFullFileName));
//writer.setPageEvent(new EndPage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
docMyDocument.open ();
//add the page title
if(strTableType.equalsIgnoreCase("custom"))
docMyDocument = AddTitle(docMyDocument, "Report");
else
docMyDocument = AddTitle(docMyDocument, strTableType);
try {
docMyDocument = AddTable(docMyDocument, alChartData, strTableType, alCharts);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
docMyDocument.close();
} /**********end calls to individual page construction functions **********************/
/**********the following code creates the main table and calls for the creation of the sub-tables ******/
*/private static Document AddTable(Document docMyDocument, ArrayList alTableData, String strChartType, ArrayList alCharts) throws SQLException, DocumentException{
PdfPTable PdfPStatTable = null;
PdfPTable PdfPImageTable = null;
int iCounter =0;
ArrayList alIndividualTableData = new ArrayList();
String[] strarBuffer = null;
int k=0;
String strPreviousMouseID = null;
System.out.println("Creating PDF table with " + alTableData.size() + " elements.");
if (strChartType.equalsIgnoreCase("custom")){
PdfPTable pdfTable = new PdfPTable(7);
pdfTable.setWidthPercentage(45);
pdfTable.setHeaderRows(1);
//create the seperate arraylists for each chart
for (iCounter = 0; iCounter < alTableData.size() ; iCounter ++){
strarBuffer = (String[])alTableData.get(iCounter);
String[] strarBuffer2 = {strarBuffer[0], strarBuffer[2],strarBuffer[3],strarBuffer[4],strarBuffer[5],strarBuffer[6], strarBuffer[8]};
if(((iCounter !=0) && (strPreviousMouseID.equals(strarBuffer[1])!=true))||(iCounter==alTableData.size()-1)){
//alIndividualTableData.add(alBuffer);
PdfPStatTable = CreateCustomTable(pdfTable, docMyDocument, alIndividualTableData);
PdfPImageTable = AddChart(docMyDocument, (JFreeChart)alCharts.get(k));
PdfPTable pdfPresentationTable = new PdfPTable(2);
pdfPresentationTable.setWidthPercentage(100);
pdfPresentationTable.setSpacingAfter(2f);
PdfPCell pdfCell;
pdfCell = new PdfPCell(PdfPStatTable);
pdfCell.setVerticalAlignment(Element.ALIGN_CENTER);
pdfCell.setPadding(2f);
pdfCell.setBorder(1);
pdfPresentationTable.addCell(pdfCell);
pdfCell = new PdfPCell(PdfPImageTable);
pdfCell.setVerticalAlignment(Element.ALIGN_CENTER);
pdfCell.setPadding(2f);
pdfCell.setBorder(1);
pdfPresentationTable.addCell(pdfCell);
docMyDocument.add(pdfPresentationTable);
GlobalDataStore.logger.debug("Adding Page " + (k+1));
docMyDocument.newPage();
alIndividualTableData.clear();
alIndividualTableData.add(strarBuffer2);
pdfPresentationTable.flushContent();
PdfPStatTable.flushContent();
PdfPStatTable.flushContent();
writer.flush();
k++;
}
else{
alIndividualTableData.add(strarBuffer2);
}
strPreviousMouseID = strarBuffer[1];
}
}
/////more code that doesn't get called in this case ///////////////////////////////
/*************function that fills in the table*********************************/
private static PdfPTable CreateCustomTable(PdfPTable pdfTable, Document docMyDocument, ArrayList alTableData){
int iCounter =0;
Double dblMissPercentage;
DecimalFormat dfPercent = new DecimalFormat("0.0%");
PdfPCell pdfCell = null;
Font headerFont = new Font (Font.HELVETICA, 12, Font.BOLD, new Color (0, 0, 0));
//add the column headers
for(iCounter =0; iCounter < strArColumnHeaders.length; iCounter++)
{
pdfCell = new PdfPCell(new Paragraph(strArColumnHeaders[iCounter], headerFont));
GlobalDataStore.logger.debug("Header " + strArColumnHeaders[iCounter]);
pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.setPadding(2);
pdfCell.setBackgroundColor(Color.LIGHT_GRAY);
pdfTable.addCell(pdfCell);
}
for (int i = 0; i< alTableData.size(); i ++)
{
dblMissPercentage = Double.valueOf(((String[])alTableData.get(i))[6]);
for(int j=0; j < strArColumnHeaders.length; j++){
pdfCell.setPadding(2);
if(j==0)
pdfCell.setHorizontalAlignment(Element.ALIGN_LEFT);
else
pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
if(j==6)
pdfTable.addCell(dfPercent.format(dblMissPercentage));
else{
pdfCell = new PdfPCell(new Paragraph(((String[])alTableData.get(i))[j]));
pdfTable.addCell(pdfCell);
}
}
}
return pdfTable;
}