To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository
Bullets.java
Output
Reference : iText Website
Maven Dependency
2.
<
dependency
>
3.
<
groupid
>com.itextpdf</
groupid
>
4.
<
artifactid
>itextpdf</
artifactid
>
5.
<
version
>5.5.11</
version
>
6.
</
dependency
>
7.
Bullets.java
01.
package
com.javatutorialcorner.itextpdf;
02.
03.
import
com.itextpdf.text.Chunk;
04.
import
com.itextpdf.text.Document;
05.
import
com.itextpdf.text.DocumentException;
06.
import
com.itextpdf.text.Font;
07.
import
com.itextpdf.text.Font.FontFamily;
08.
import
com.itextpdf.text.Paragraph;
09.
import
com.itextpdf.text.Phrase;
10.
import
com.itextpdf.text.pdf.BaseFont;
11.
import
com.itextpdf.text.pdf.PdfWriter;
12.
13.
14.
15.
import
java.io.File;
16.
import
java.io.FileOutputStream;
17.
import
java.io.IOException;
18.
19.
public
class
Bullets {
20.
21.
public
static
final
String DEST =
"C:/JTC/Bullets.pdf"
;
22.
public
static
final
String FONT =
"C:/JTC/FreeSans.ttf"
;
23.
public
static
final
String[] ITEMS = {
24.
"Insurance system"
,
"Agent"
,
"Agency"
,
"Agent Enrollment"
,
"Agent Settings"
,
25.
"Appointment"
,
"Continuing Education"
,
"Hierarchy"
,
"Recruiting"
,
"Contract"
,
26.
"Message"
,
"Correspondence"
,
"Licensing"
,
"Party"
27.
};
28.
29.
public
static
void
main(String[] args)
throws
IOException, DocumentException {
30.
File file =
new
File(DEST);
31.
file.getParentFile().mkdirs();
32.
new
Bullets().createPdf(DEST);
33.
}
34.
35.
36.
public
void
createPdf(String dest)
throws
DocumentException, IOException {
37.
Document document =
new
Document();
38.
PdfWriter.getInstance(document,
new
FileOutputStream(dest));
39.
document.open();
40.
Font zapfdingbats =
new
Font(FontFamily.ZAPFDINGBATS,
8
);
41.
Font font =
new
Font();
42.
Chunk bullet =
new
Chunk(String.valueOf((
char
)
108
), zapfdingbats);
43.
44.
Paragraph p =
new
Paragraph(
"Items can be split if they don't fit at the end: "
, font);
45.
for
(String item: ITEMS) {
46.
p.add(bullet);
47.
p.add(
new
Phrase(
" "
+ item +
" "
, font));
48.
}
49.
document.add(p);
50.
document.add(Chunk.NEWLINE);
51.
52.
p =
new
Paragraph(
"Items can't be split if they don't fit at the end: "
, font);
53.
for
(String item: ITEMS) {
54.
p.add(bullet);
55.
p.add(
new
Phrase(
"\u00a0"
+ item.replace(
' '
,
'\u00a0'
) +
" "
, font));
56.
}
57.
document.add(p);
58.
document.add(Chunk.NEWLINE);
59.
60.
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
61.
Font f =
new
Font(bf,
12
);
62.
p =
new
Paragraph(
"Items can't be split if they don't fit at the end: "
, f);
63.
for
(String item: ITEMS) {
64.
p.add(
new
Phrase(
"\u2022\u00a0"
+ item.replace(
' '
,
'\u00a0'
) +
" "
, f));
65.
}
66.
document.add(p);
67.
68.
document.close();
69.
}
70.
}
Output
Reference : iText Website
0 comments:
Post a Comment