To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository
OrdinalNumbers.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.
OrdinalNumbers.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.pdf.PdfWriter;
10.
11.
12.
import
java.io.File;
13.
import
java.io.FileOutputStream;
14.
import
java.io.IOException;
15.
16.
public
class
OrdinalNumbers {
17.
public
static
final
String DEST =
"C:/JTC/OrdinalNumbers.pdf"
;
18.
19.
public
static
void
main(String[] args)
throws
IOException, DocumentException {
20.
File file =
new
File(DEST);
21.
file.getParentFile().mkdirs();
22.
new
OrdinalNumbers().createPdf(DEST);
23.
}
24.
public
void
createPdf(String dest)
throws
IOException, DocumentException {
25.
Document document =
new
Document();
26.
PdfWriter.getInstance(document,
new
FileOutputStream(dest));
27.
document.open();
28.
Font small =
new
Font(FontFamily.HELVETICA,
6
);
29.
Chunk st =
new
Chunk(
"st"
, small);
30.
st.setTextRise(
7
);
31.
Chunk nd =
new
Chunk(
"nd"
, small);
32.
nd.setTextRise(
7
);
33.
Chunk rd =
new
Chunk(
"rd"
, small);
34.
rd.setTextRise(
7
);
35.
Chunk th =
new
Chunk(
"th"
, small);
36.
th.setTextRise(
7
);
37.
Paragraph first =
new
Paragraph();
38.
first.add(
"The 1"
);
39.
first.add(st);
40.
first.add(
" of May"
);
41.
document.add(first);
42.
Paragraph second =
new
Paragraph();
43.
second.add(
"The 2"
);
44.
second.add(nd);
45.
second.add(
" and the 3"
);
46.
second.add(rd);
47.
second.add(
" of June"
);
48.
document.add(second);
49.
Paragraph fourth =
new
Paragraph();
50.
fourth.add(
"The 4"
);
51.
fourth.add(rd);
52.
fourth.add(
" of July"
);
53.
document.add(fourth);
54.
document.close();
55.
}
56.
}
Output
Reference : iText Website
0 comments:
Post a Comment