I am using iText 1.4.5, jdk 1.5 and viewing generated document with
Microsoft Word 2002 SP2.
What I need to do is create RTF documents with some tables in it. The
tables have a particular look and feel. I need the table background to
be light gray, with some spacing between cells and some cells with white
background.
The problem is I can't set the color I want to the the space in between
the cells.
I send some code so you can have better ideia about what I want to do
and what I am doing:
public static void main(String[] args) {
Date datInic = new Date();
System.out.println(new SimpleDateFormat("dd/MM/yyyy
hh:mm.ss").format(datInic)+" - Begin");
try {
// Create de document
Document document = new Document();
RtfWriter2.getInstance(document, new
FileOutputStream("TesteTabelas.rtf"));
document.open();
// Register, create and set fonts
FontFactory.register("c:\\windows\\fonts\\verdana.ttf");
FontFactory.register("c:\\windows\\fonts\\arial.ttf");
RtfParagraphStyle.STYLE_NORMAL.setFontName("verdana");
RtfParagraphStyle.STYLE_NORMAL.setSize(8);
Font arial8 = new Font(FontFactory.getFont("arial"));
arial8.setSize(8);
arial8.setColor(Color.BLACK);
Paragraph paragraph;
Cell cell;
// Define cell widths
int[] cellWidths = new int[]{ 7, 26, 11, 11, 15, 5, 26};
int varNumber = 1;
// Create table and set default settings
Table table = new Table(cellWidths.length);
// setBackgroundColor seems to be ignored
table.setBackgroundColor(Color.LIGHT_GRAY);
table.setSpacing(3);
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setWidth(100);
table.setWidths(cellWidths);
//-----------------------------------------------------
// 1st label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(4);
table.addCell(cell);
paragraph = new Paragraph("An image here,\r\nno problem with
this.", arial8);
paragraph.setAlignment(Paragraph.ALIGN_CENTER);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
cell.setRowspan(10);
table.addCell(cell);
//-----------------------------------------------------
// 1st field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(4);
table.addCell(cell);
//-----------------------------------------------------
// 2nd label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(6);
table.addCell(cell);
//-----------------------------------------------------
// 2nd field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(6);
table.addCell(cell);
//-----------------------------------------------------
// 3rd label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(4);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
//-----------------------------------------------------
// 3rd field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(4);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
//-----------------------------------------------------
// 4th label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
//-----------------------------------------------------
// 4th field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
//-----------------------------------------------------
// 5th label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
//-----------------------------------------------------
// 5th field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
table.addCell(cell);
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(2);
table.addCell(cell);
//-----------------------------------------------------
// 6th label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(7);
table.addCell(cell);
//-----------------------------------------------------
// 6th field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(7);
table.addCell(cell);
//-----------------------------------------------------
// 7th label row
//-----------------------------------------------------
paragraph = new Paragraph("Var "+varNumber++, arial8);
cell = new Cell(paragraph);
cell.setBorder(0);
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(7);
table.addCell(cell);
//-----------------------------------------------------
// 7th field row
//-----------------------------------------------------
paragraph = new Paragraph("", arial8);
cell = new Cell(paragraph);
cell.setBackgroundColor(Color.WHITE);
cell.setVerticalAlignment(Cell.ALIGN_BOTTOM);
cell.setColspan(7);
table.addCell(cell);
// Add table to document
document.add(table);
document.close();
System.out.println(new SimpleDateFormat("dd/MM/yyyy
hh:mm.ss").format(new Date())+" - End");
System.out.println("Elapsed time = "+(new Date().getTime() -
datInic.getTime())+" ms");
} catch(Exception e) {
e.printStackTrace();
}
}
Thank you,
Marcel Horner
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/