Reputation: 175
I'm pretty sure the easiest answer to my problem is going to be "You need to update your version of iText." However, my boss is unwilling to update after updating from iText 4 to iText 5, as he put it, "Broke so much crap." So I'm using iText 5.0.1 to build this PDF.
I am building an application that will read in an Excel file and create badges for an event we take part of. It's fairly straightforward, and everything works as expected, except for the way the end result looks.
The way I have the PDF set up (code will be shown below) is that each badge is printed with a front and back (we print double sided), and each page itself is supposed to hold 6 badges. When I tested this by copying the same badge into each sell, it looked fine. But when I use a set of data, for whatever reason, my PDF is thrown off. Instead of 6 badges per page, it will sometimes print 6 per page, other times it'll print 3 per page. For example, my excel file has 21 lines of data - 21 badges to create. I get 3 sets of 3 badges per page, and 2 sets of 6 badges per page, set up as 3,6,6,3 respective to the number of badges on each page.
Is there something I am missing that would result in 3 pages of 6 badges each, followed by a page of just 3 badges?
Document document = new Document(PageSize.LETTER, MARGIN, MARGIN, MARGIN, MARGIN);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
buildDocument(document, badgeFronts, badgeBacks);
document.close();
public void buildDocument(Document document, ArrayList<PdfPTable> badgeFronts, ArrayList<PdfPTable> badgeBacks) throws DocumentException{
float[] frontTableWidths = {150f, 10f, 150f, 10f, 150f, 10f};
float[] backTableWidths = {10f, 150f, 10f, 150f, 10f, 150f};
PdfPTable frontTable = new PdfPTable(6);
frontTable.setWidths(frontTableWidths);
frontTable.setWidthPercentage(100);
PdfPTable backTable = new PdfPTable(6);
backTable.setWidths(backTableWidths);
backTable.setWidthPercentage(100);
int count = 0;
for(int i = 0; i < badgeFronts.size(); i++){
PdfPCell frontCell = new PdfPCell();
frontCell.setFixedHeight(260f);
frontCell.setBorderColor(BaseColor.GRAY);
PdfPCell backCell = new PdfPCell();
backCell.setFixedHeight(260f);
backCell.setBorderColor(BaseColor.GRAY);
if(count == 5){
count = 0;
document.add(frontTable);
document.newPage();
document.add(backTable);
document.newPage();
frontTable.deleteBodyRows();
backTable.deleteBodyRows();
}
PdfPTable front = badgeFronts.get(i);
PdfPTable back = badgeBacks.get(i);
frontCell.addElement(front);
backCell.addElement(back);
frontTable.addCell(frontCell);
frontTable.addCell("");
backTable.addCell("");
backTable.addCell(backCell);
if(i == badgeFronts.size() - 1){
document.add(frontTable);
document.newPage();
document.add(backTable);
document.newPage();
}
count++;
}
frontTable.completeRow();
backTable.completeRow();
}
Above is my code. I create the Tables that live in the main table's cells elsewhere and can show that if needed, however I don't believe it is necessary.
Upvotes: 0
Views: 116
Reputation: 175
if(count == 5){
count = 0;
document.add(frontTable);
document.newPage();
document.add(backTable);
document.newPage();
frontTable.deleteBodyRows();
backTable.deleteBodyRows();
}
This block of code is the issue. For starters, it's too early in the loop. It needs to be placed after the count++;
statement. Second, instead of checking for count == 5
after moving the block, checking for count == 6
did the trick. As soon as this change happened, it started working exactly as it was expected to.
This pretty much means I'm not great at exhausting all my options before turning to Stack Overflow...
Upvotes: 1