Saturday, June 15, 2013

Create a pdf

With the CreatePdf program, you learn how to create a pdf document and write a few lines of text in it. In Java, you can simply create a pdf document by using iText library. It is free and small. To use the iText library to create pdf documents, you need to download the library zip file from http://itextpdf.com/download.php. Then extract the zip file and add the jar file (e.g. itextpdf-5.4.2.jar) to your CLASSPATH of your operating system. For the Eclipse users, you can add the jar file to your project by clicking the Add External JARs... in the Libraries tab of the New Java Project window.

add itext library in eclipse


Width iText, you can create a pdf in a few steps as shown below:
1. Create a document object
2. Create a pdf writer object
3. Open the document
4. Add text to the document by using Chunk, Phrase, or Paragraph object
5. Close the document

CreatePdf source code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class CreatePdf {
public static void main(String[] args){
createPdf();
}
public static void createPdf(){
try{
//create a document object with paper size A4 and
//margin left=72 units, margin right=72 units, margin top=72 units, and margin bottom=72 units
//72 units=1 inch
Document doc=new Document(PageSize.A4,72,72,72,72);
//create a pdf writer object to write text to mypdf.pdf file
PdfWriter pwriter=PdfWriter.getInstance(doc, new FileOutputStream("mypdf.pdf"));
//specify the vertical space between the lines of text
pwriter.setInitialLeading(20);
//open the document so it is ready to write text to the pdf file
doc.open();
//create font objects for the chunks of text to be written to the file
Font fontNormalBlack=new Font(FontFamily.TIMES_ROMAN, 20, Font.NORMAL, BaseColor.BLACK);
Font fontBoldWhite=new Font(FontFamily.TIMES_ROMAN, 20, Font.BOLD, BaseColor.WHITE);
//create chunk objects for text with specific fonts
Chunk chObj1=new Chunk("Hello World",fontNormalBlack);
Chunk chObj2=new Chunk("This pdf document is created with iText.",fontBoldWhite);
chObj2.setBackground(BaseColor.BLACK); //set background to the chunk
//add the chunk objects to the document
doc.add(chObj1);
doc.add(Chunk.NEWLINE); //add a new line between the chunks
doc.add(chObj2);
//close the document when writing finishes
doc.close();
//open the pdf file
try{

if(Desktop.isDesktopSupported()){
File f=new File("mypdf.pdf");
Desktop.getDesktop().open(f);

}
}catch(IOException ie){}

}catch(DocumentException de){ de.printStackTrace();}
catch(FileNotFoundException fnfe){fnfe.printStackTrace();}
}
}

create pdf file in Java

For code explanation, please read comments along with the code.

2 comments:

  1. You can also use Java Library for PDF form Aspose to create PDF File in java. This Lib offers many other features which you explore and get an idea about how to use them.

    ReplyDelete
  2. nice post regarding creating the pdf document. I liked the post . keep sharing . Thank you Buzz App

    ReplyDelete