I am trying to get dotted border (used cell border) in middle of the table by using iText 2.1.0. Below code would be generating dotted border even after middle of the table.
Could you please help me on this to add event for a specific cell alone?
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class DottedLine {
public static final String RESULT = "Dotted.pdf";
public static void main(String[] args) throws Exception {
new DottedLine().createPdf(RESULT);
}
public void createPdf(String filename) throws Exception {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfPTable tab = new PdfPTable(1);
for (int j = 1; j <= 10; j++) {
if (j == 5) {
PdfPCell c1 = new PdfPCell(new Phrase("Test" + j));
c1.setBorder(Rectangle.TOP);
c1.setCellEvent(new dot());
tab.addCell(c1);
} else {
PdfPCell c2 = new PdfPCell(new Phrase("Test " + j));
c2.setBorder(Rectangle.TOP);
tab.addCell(c2);
}
}
document.add(tab);
document.close();
}
// Cell Event to make dotted Line
class dot implements PdfPCellEvent {
@Override
public void cellLayout(PdfPCell arg0, Rectangle arg1, PdfContentByte[] arg2) {
arg0.setBorder(Rectangle.TOP);
PdfContentByte cb = arg2[PdfPTable.LINECANVAS];
cb.setLineDash(3f, 3f);
cb.restoreState();
}
}
}