import
com.itextpdf.kernel.color.Color;
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.Table;
import
com.itextpdf.layout.property.TextAlignment;
class
PersonalInfo {
String name;
int
age;
String origin;
PersonalInfo(String name,
int
age, String origin)
{
this
.name = name;
this
.age = age;
this
.origin = origin;
}
}
public
class
TableBackground {
public
static
void
main(String[] args)
{
try
{
String path =
"F:/Table.pdf"
;
PdfWriter writer =
new
PdfWriter(path);
PdfDocument pdf =
new
PdfDocument(writer);
Document doc =
new
Document(pdf);
Table table =
new
Table(
3
);
Cell c1 =
new
Cell();
Cell c2 =
new
Cell();
Cell c3 =
new
Cell();
c1.add(
"Name"
);
c2.add(
"Age"
);
c3.add(
"Origin"
);
c1.setBackgroundColor(Color.GREEN);
c2.setBackgroundColor(Color.GREEN);
c3.setBackgroundColor(Color.GREEN);
c1.setTextAlignment(TextAlignment.CENTER);
c2.setTextAlignment(TextAlignment.CENTER);
c3.setTextAlignment(TextAlignment.CENTER);
table.addCell(c1);
table.addCell(c2);
table.addCell(c3);
PersonalInfo[] personalInfo
=
new
PersonalInfo[
3
];
personalInfo[
0
]
=
new
PersonalInfo(
"Yuri"
,
37
,
"Russian"
);
personalInfo[
1
]
=
new
PersonalInfo(
"Diksha"
,
18
,
"Indian"
);
personalInfo[
2
]
=
new
PersonalInfo(
"Sergio"
,
34
,
"Italian"
);
for
(PersonalInfo pi : personalInfo) {
c1 =
new
Cell();
c2 =
new
Cell();
c3 =
new
Cell();
if
(pi.origin.equals(
"Indian"
)) {
c1.setBackgroundColor(Color.ORANGE,
10
);
c2.setBackgroundColor(Color.WHITE);
c3.setBackgroundColor(Color.GREEN);
}
else
if
(pi.origin.equals(
"Russian"
)) {
c1.setBackgroundColor(Color.WHITE);
c2.setBackgroundColor(Color.BLUE);
c3.setBackgroundColor(Color.RED);
}
else
if
(pi.origin.equals(
"Italian"
)) {
c1.setBackgroundColor(Color.GREEN);
c2.setBackgroundColor(Color.WHITE);
c3.setBackgroundColor(Color.RED);
}
c1.add(pi.name);
c2.add(pi.age +
""
);
c3.add(pi.origin);
c1.setTextAlignment(TextAlignment.CENTER);
c2.setTextAlignment(TextAlignment.CENTER);
c3.setTextAlignment(TextAlignment.CENTER);
table.addCell(c1);
table.addCell(c2);
table.addCell(c3);
}
doc.add(table);
doc.close();
System.out.println(
"Table Created"
);
}
catch
(Exception e) {
System.err.println(e);
}
}
}