PdfWriter.getInstance(document, new FileOutputStream("tableattributes.pdf"));
RtfWriter2.getInstance(document, new FileOutputStream("tableattributes.rtf"));
// open the document
document.open();
// add content
SimpleTable table = new SimpleTable();
table.setCellpadding(5);
table.setCellspacing(8);
SimpleCell row = new SimpleCell(SimpleCell.ROW);
row.setBackgroundColor(Color.yellow);
SimpleCell cell = new SimpleCell(SimpleCell.CELL);
cell.setWidth(100f);
cell.add(new Paragraph("rownumber"));
row.add(cell);
cell = new SimpleCell(SimpleCell.CELL);
cell.setWidth(50f);
cell.add(new Paragraph("A"));
row.add(cell);
cell = new SimpleCell(SimpleCell.CELL);
cell.setWidth(50f);
cell.add(new Paragraph("B"));
row.add(cell);
cell = new SimpleCell(SimpleCell.CELL);
cell.setWidth(50f);
cell.add(new Paragraph("C"));
row.add(cell);
table.addElement(row);
for (int i = 0; i < 100; i++) {
row = new SimpleCell(SimpleCell.ROW);
switch (i % 3) {
case 0:
row.setBackgroundColor(Color.red);
break;
case 1:
row.setBackgroundColor(Color.green);
break;
case 2:
row.setBackgroundColor(Color.blue);
break;
}
if (i % 2 == 1) {
row.setBorderWidth(3f);
}
cell = new SimpleCell(SimpleCell.CELL);
cell.add(new Paragraph("Row " + (i + 1)));
cell.setSpacing_left(20f);
row.add(cell);
if (i % 5 == 4) {
cell = new SimpleCell(SimpleCell.CELL);
cell.setColspan(3);
cell.setBorderColor(Color.orange);
cell.setBorderWidth(5f);
cell.add(new Paragraph("Hello!"));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
row.add(cell);
}
else {
cell = new SimpleCell(SimpleCell.CELL);
cell.add(new Paragraph("A"));
row.add(cell);
cell = new SimpleCell(SimpleCell.CELL);
cell.add(new Paragraph("B"));
cell.setBackgroundColor(Color.gray);
row.add(cell);
cell = new SimpleCell(SimpleCell.CELL);
cell.add(new Paragraph("C"));
row.add(cell);
}
table.addElement(row);
}
document.add(table);
}
catch(Exception e) {
e.printStackTrace();