You are on page 1of 4

CODE - Create a PDF File via the iText Library

The below class uses the iText library to create PDF files. The code is neither part of a java mapping nor an adapter module but is a
sample that will help the reader as a reference when he/she is required to custom code in !/P!.
"ssumptions#
$. %essageTransform&ean converts the target %' to a F'"T format. The field separator is comma and the end of each record can be
identified by the string literal ()*D(
+. The iText library is downloaded and imported for the project.
)g. !nput format#$,,-"na.in /.ywal.er-)"/-0onsultant)*D+,,-Darth 1ader-2ava-/r. Programmer)*D3,,-4bi56an
7enobi-%/Techmologies-Project %anager)*D 8ere there are 3 records and 9 fields per record.
The output PDF will have a table which will have a %ain 8eader- followed by the header for each column of the data followed by the data
itself. ! have tried to experiment with some colors. Forgive my taste when it comes to being an aesthetic
The below is the code that will try capture the above requirements-
/*
This is a java class to create a PDF document out of a given string
Created By Shabarish Vijayaumar on !ay "#$ %&&'
*/
im(ort java)a*t)Color+
im(ort java)io)File,ut(utStream+
im(ort java)io)-,./ce(tion+
im(ort com)lo*agie)te/t)Document+
im(ort com)lo*agie)te/t)Document./ce(tion+
im(ort com)lo*agie)te/t).lement+
im(ort com)lo*agie)te/t)Paragra(h+
im(ort com)lo*agie)te/t)(df)PdfPCell+
im(ort com)lo*agie)te/t)(df)PdfPTable+
im(ort com)lo*agie)te/t)(df)Pdf0riter+
(ublic class createPDF 1
(ublic static void main2String34 args5 1
// 6ssume the belo* is the in(ut file format
String in(ut 7
8"&&96nain Sy*aler9.6S9Consultant.:D%&&9Darth Vader9;ava9Sr)
Programmer.:D<&&9,bi=0an >enobi9!STechnologies9Project !anager.:D8+
// creation of a document=object
Document document 7 ne* Document25+
try 1
// create a *riter
Pdf0riter)get-nstance2
// that listens to the document
document9
// and directs a PDF=stream to a file
ne* File,ut(utStream28out(ut)(df855+
// o(en the document
document)o(en25+
// add a table to the document
PdfPTable table 7 ne* PdfPTable2?5+
PdfPCell cell 7
ne* PdfPCell2
ne* Paragra(h28.m(loyee Details for @AB ,rganiCation855+
cell)setCols(an2?5+
cell)setBacgroundColor2Color)red5+
cell)setDoriContal6lignment2.lement)6E-F:GC.:T.H5+
table)addCell2cell5+
//Set Deader Te/t for the Table
cell 7 ne* PdfPCell2ne* Paragra(h28.!P :,855+
cell)setBacgroundColor2Color)blue5+
table)addCell2cell5+
cell 7 ne* PdfPCell2ne* Paragra(h28:6!.855+
cell)setBacgroundColor2Color)blue5+
table)addCell2cell5+
cell 7 ne* PdfPCell2ne* Paragra(h28D.P6HT!.:T855+
cell)setBacgroundColor2Color)blue5+
table)addCell2cell5+
cell 7 ne* PdfPCell2ne* Paragra(h28D.S-F:6T-,:855+
cell)setBacgroundColor2Color)blue5+
table)addCell2cell5+
//Fill data to the table
String in(ut6rray34 7 in(ut)s(lit28.:D85+
for 2int i 7 &+ i I in(ut6rray)length+ iJJ5 1
String fieldValues6rray34 7 in(ut6rray3i4)s(lit28985+
for 2int j 7 &+ j I fieldValues6rray)length+ jJJ5 1
table)addCell2fieldValues6rray3j4)toString255+
K
K
document)add2table5+
K catch 2Document./ce(tion de5 1
System)err)(rintln2de)get!essage255+
K catch 2-,./ce(tion ioe5 1
System)err)(rintln2ioe)get!essage255+
K
// close the document
document)close25+
K
K

/o now this is what the final output will loo. li.e:
Note: When coding for the module, write out to a Outputstream instead of a FileOutputStream
'abels parameters

You might also like