Friday, 8 August 2014

Create high quality PDF file using JAVA (In 7 easy steps). [Tags: #Java, #iText, #PDF, #PDFUsingJava, #SimpleJavaPro]

Welcome again,

         Creating .pdf file using java is made simple now by iText. If you have basic knowledge of Java this tutorial is sufficient for you to create PDF file using java.
Let's learn now.


Requirements:

1. JDK 1.6 or 1.7.

   If not installed, download and install it.
Download JDK here.

2. Eclipse IDE.

   If not installed, download and install it.
Download Eclipse here.

3. Basic knowledge of Java.

     If you do not have,learn basics.
Learn Basic Java here.

(Note: Before starting this tutorial I am assuming that all requirements are fulfilled by you.)



Step 1: Download iText jar file from link given below.

Download: iText jar files


  • After download extract them anywhere,they will be required further.

Step 2: Open Eclipse IDE click on "File>New>Java Project" and name your project.



  • Click on Finish.


Step 3: Buildpath jar file to your project by: "Right click on project>Build Path>Configure Build Path>Add External Jar".


  • Select jar files from wherever you have extracted all iText jar files.


Step 4: Create class,name it  'CreatePDF' (or whatever).


  • Click on finish.                                                                                                                                 


Step 5: Code in CreatePDF.java .



import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class CreatePDF {

private static String File = "D:/FirstPdf.pdf";
private static Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
     Font.BOLD);

public static void main(String[] args) throws FileNotFoundException,
DocumentException, MalformedURLException {

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(File));

document.open();
Paragraph preface = new Paragraph();

preface.add(new Paragraph("Hello World...",titleFont));
preface.add(new Paragraph("This tutorial if made by SimpleJavaPro™"));

document.add(preface);
document.newPage();
document.close();
System.out.println("Pdf is created.");
}

}




Step 6: Run  CreatePDF.java by "Right click on CreatePDF>Run As>Java Application".

  • If everything goes correct you will see on command prompt like this...


Step 7: See PDF is created in 'D:/' drive...and smile...:-)....



PDFUsingJava

Note:
  • Now you know that how to create simple pdf using this tutorial.
  • For creating high quality pdfs find iText tutorials on Google.
  • Using This iText you can also create your high class resume.
  • Stay connected with us...thanks...:-)

No comments:

Post a Comment