Pages

Tuesday, 10 September 2013

Creating PDF with Java and iText, Generating PDF Using Java Example



Its very important in real time to convert the records or any statements into PDF,EXCEL,Word,TXT and some other formats too :-)now i will tell you how to generate PDF using iTextPdf API (simple API to generate PDFs in java), am going to cover…
  • Inserting Image in PDF
  • Inserting Table in PDF
  • Inserting List in PDF
  • Text formatting in PDF
  • Adding new Pages in PDF
  • Little Chunk
But before you start this application you must download iTextpdf related jar(s).

PdfGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGen {

    public static void main(String[] args) {

        try {

              OutputStream file = new FileOutputStream(new File("C:\\PDF_Java4s.pdf"));
              Document document = new Document();
              PdfWriter.getInstance(document, file);

            //Inserting Image in PDF
                 Image image = Image.getInstance ("src/pdf/java4s.png");
                 image.scaleAbsolute(120f, 60f);//image width,height    

            //Inserting Table in PDF
                 PdfPTable table=new PdfPTable(3);

                         PdfPCell cell = new PdfPCell (new Paragraph ("Java4s.com"));

                      cell.setColspan (3);
                      cell.setHorizontalAlignment (Element.ALIGN_CENTER);
                      cell.setPadding (10.0f);
                      cell.setBackgroundColor (new BaseColor (140, 221, 8));                                  

                      table.addCell(cell);                                      

                      table.addCell("Name");
                      table.addCell("Address");
                      table.addCell("Country");
                      table.addCell("Java4s");
                      table.addCell("NC");
                      table.addCell("United States");
                      table.setSpacingBefore(30.0f);       // Space Before table starts, like margin-top in CSS
                      table.setSpacingAfter(30.0f);        // Space After table starts, like margin-Bottom in CSS                                          

             //Inserting List in PDF
                      List list=new List(true,30);
                      list.add(new ListItem("Java4s"));
                      list.add(new ListItem("Php4s"));
                      list.add(new ListItem("Some Thing..."));        

             //Text formating in PDF
                    Chunk chunk=new Chunk("Welecome To Java4s Programming Blog...");
                    chunk.setUnderline(+1f,-2f);//1st co-ordinate is for line width,2nd is space between
                    Chunk chunk1=new Chunk("Php4s.com");
                    chunk1.setUnderline(+4f,-8f);
                    chunk1.setBackground(new BaseColor (17, 46, 193));      

             //Now Insert Every Thing Into PDF Document
                 document.open();//PDF document opened........                  

                    document.add(image);

                    document.add(Chunk.NEWLINE);   //Something like in HTML :-)

                    document.add(new Paragraph("Dear Java4s.com"));
                    document.add(new Paragraph("Document Generated On - "+new Date().toString()));    

                    document.add(table);

                    document.add(chunk);
                    document.add(chunk1);

                    document.add(Chunk.NEWLINE);   //Something like in HTML :-)                                

                       document.newPage();            //Opened new page
                    document.add(list);            //In the new page we are going to add list

                 document.close();

                         file.close();

            System.out.println("Pdf created successfully..");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Explanation
  • Point number 1, just forget every thing :-)its very simple program, i will show you the road map
  • First open OutputStream and give our output PDF file name [ line number 26 ]
  • Create Document class object [ line number 27 ]
  • Call getInstance() static method from PdfWriter class
  • In getInstance() we have 2 parameters, 1 is document object and 2nd one is our OutputStream object
  • Now jump to line number 70, we are calling open() method from Document object, means think PDF is opened now
  • Come to line number 30, here am going to write some code to insert an image into the PDF document
  • Create Image class object and loaded the image and adjusted the dimensions
  • Come to line number 72, add your image object here :-)
  • line number 85 we have created new PDF page
  • Similarly allllllllllllllll

No comments:

Post a Comment