import
java.io.*;
import
com.itextpdf.kernel.colors.*;
import
com.itextpdf.kernel.pdf.PdfDocument;
import
com.itextpdf.kernel.pdf.PdfWriter;
import
com.itextpdf.layout.Document;
import
com.itextpdf.layout.element.Cell;
import
com.itextpdf.layout.element.Paragraph;
import
com.itextpdf.layout.element.Table;
import
com.itextpdf.layout.property.TextAlignment;
public
class
FormatCellPdf {
public
static
void
main(String args[])
throws
Exception
{
String path = System.getProperty(
"user.dir"
);
path +=
"/cellPDF.pdf"
;
PdfWriter writer =
new
PdfWriter(path);
PdfDocument pdfDoc =
new
PdfDocument(writer);
Document doc =
new
Document(pdfDoc);
float
[] columnWidhts = { 300F, 300F };
Table table =
new
Table(columnWidhts);
Cell cell1 =
new
Cell();
cell1.add(
new
Paragraph(
"Color"
).setFontColor(
ColorConstants.WHITE));
cell1.setBackgroundColor(ColorConstants.BLACK);
cell1.setTextAlignment(TextAlignment.CENTER);
Cell cell2 =
new
Cell();
cell2.add(
new
Paragraph(
"Black"
).setFontColor(
ColorConstants.WHITE));
cell2.setBackgroundColor(ColorConstants.BLUE);
cell2.setTextAlignment(TextAlignment.CENTER);
Cell cell3 =
new
Cell();
cell3.add(
new
Paragraph(
"Color"
).setFontColor(
ColorConstants.WHITE));
cell3.setBackgroundColor(ColorConstants.BLUE);
cell3.setTextAlignment(TextAlignment.CENTER);
Cell cell4 =
new
Cell();
cell4.add(
new
Paragraph(
"Red"
).setFontColor(
ColorConstants.WHITE));
cell4.setBackgroundColor(ColorConstants.BLACK);
cell4.setTextAlignment(TextAlignment.CENTER);
Cell cell5 =
new
Cell();
cell5.add(
new
Paragraph(
"Color"
).setFontColor(
ColorConstants.WHITE));
cell5.setBackgroundColor(ColorConstants.BLACK);
cell5.setTextAlignment(TextAlignment.CENTER);
Cell cell6 =
new
Cell();
cell6.add(
new
Paragraph(
"Yellow"
).setFontColor(
ColorConstants.WHITE));
cell6.setBackgroundColor(ColorConstants.BLUE);
cell6.setTextAlignment(TextAlignment.CENTER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
doc.add(table);
doc.close();
System.out.println(
"Table created successfully!"
);
}
}