reggie3 (2) [Avatar] Offline
#1
First, I just want to say that iText is a great tool; however, I'm sure that's obvious by now smilie

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;
}
blowagie (284) [Avatar] Offline
#2
Re: New Tables on Page After First Duplicates First Row
A link to a small standalone example that reproduces the problem would make it easier to comment on this behavior. Now I can see that you define a header row:

pdfTable.setHeaderRows(1);

and that you are adding columnheaders

//add the column headers
for(iCounter =0; iCounter < strArColumnHeaders.length; iCounter++) { ... }

I'm not sure how the snippets fit together (that's why I'm asking for a standalone example I can experiment with), but are you sure you aren't adding the header twice? Once automatically using setHeaderRows and once "manually" with the for-loop?

This is just a first impression based on a quick glance at your code. I'd have to compile and execute the code to find out if my guess is correct.
reggie3 (2) [Avatar] Offline
#3
Re: New Tables on Page After First Duplicates First Row
That was it. I didn't realize what setHeaderRows actually did. Thanks for the help.