Hi all, Finally I have found the cause of the problem, it is due to rounding errors. Text transforms are done incrementally for each row, so even a small error is accumulated. I always work in millimeters and do the conversion to points. Here is some small sample code that shows this problem:
import java.io.FileOutputStream;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class Pru {
// Conversion factor to millimeters
static final float mm = 72/25.4f;
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("a.pdf"));
document.open();
// step 4: we create a table and add it to the document
Table tbl = new Table(3);
for (int row = 0; row < 50; row++) {
for (int col = 0; col < 3; col++) {
Cell cell = new Cell();
cell.add(new Chunk(""+row+"."+col, new
Font(Font.HELVETICA, 8)));
cell.setLeading(16-2.1f*mm);
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBackgroundColor(row % 2 == 0 ?
Color.yellow:Color.white);
tbl.addCell(cell);
}
}
document.add(tbl);
System.out.println("Output file created successfully");
} catch(Exception e) {
e.printStackTrace();
}
document.close();
}
}
I attach the output PDF file.
The problem disappers if I do this:
cell.setLeading((int)(16-2.1f*mm));
Any solution to this problem?
-----Mensaje original-----
De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] nombre de Carmona Perez, David
Enviado el: viernes, 14 de mayo de 2004 14:21
Para: Itext-Questions (E-mail)
CC: Bartolome Pascual, Ismael
Asunto: [iText-questions] Background misaligned in tables
Hi,
My problem is:
I�m creating a table with Table / PdfTable in PDF. Every line has a different
background color.
For each cell, I use the same font and it takes exactly one line.
Lines are output equally spaced and look ok, but the background is missaligned and as
we go downwards the page, the background is progressivaly more above that the
corresponding line of text.
Does anyone know the cause?
Has this happened to anybody else?
Any help will be greatly appreciated.
David
*************************************************************
Este correo ha sido procesado por el antivirus del Grupo FCC.
*************************************************************
*************************************************************
Este correo ha sido procesado por el Antivirus del Grupo FCC.
*************************************************************
*************************************************************
Este correo ha sido procesado por el Antivirus del Grupo FCC
*************************************************************
a.pdf
Description: a.pdf
