To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository
SpaceCharRatioExample.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
>
SpaceCharRatioExample.java
01.
package
com.javatutorialcorner.itextpdf;
02.
03.
import
com.itextpdf.text.Document;
04.
import
com.itextpdf.text.DocumentException;
05.
import
com.itextpdf.text.Element;
06.
import
com.itextpdf.text.Paragraph;
07.
import
com.itextpdf.text.pdf.PdfWriter;
08.
09.
10.
import
java.io.File;
11.
import
java.io.FileOutputStream;
12.
import
java.io.IOException;
13.
14.
public
class
SpaceCharRatioExample {
15.
16.
public
static
final
String DEST =
"C:/JTC/SpaceCharRatioExample.pdf"
;
17.
18.
public
static
void
main(String[] args)
throws
IOException, DocumentException {
19.
File file =
new
File(DEST);
20.
file.getParentFile().mkdirs();
21.
new
SpaceCharRatioExample().createPdf(DEST);
22.
}
23.
24.
public
void
createPdf(String dest)
throws
IOException, DocumentException {
25.
// step 1
26.
Document document =
new
Document();
27.
// step 2
28.
PdfWriter writer = PdfWriter.getInstance(document,
new
FileOutputStream(dest));
29.
// step 3
30.
document.open();
31.
// step 4
32.
writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
33.
Paragraph paragraph =
new
Paragraph();
34.
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
35.
paragraph.setIndentationLeft(
20
);
36.
paragraph.setIndentationRight(
20
);
37.
paragraph.add(
"HelloWorld HelloWorld HelloWorld HelloWorld HelloWorld"
+
38.
"HelloWorld HelloWorldHelloWorldHelloWorldHelloWorld"
+
39.
"HelloWorld HelloWorld HelloWorld HelloWorldHelloWorldHelloWorld"
);
40.
document.add(paragraph);
41.
// step 5
42.
document.close();
43.
}
44.
}
Output
Reference : iText Website
0 comments:
Post a Comment