Create PDF using Java

Java also Provide the way with the help of that you can Create the PDF as per your need.

For that all you need to do is to Download the Jar file called the iText and make sure that you are going to Import it in the Right way.

i ll be providing the code with the help of that you can come to know that how you can Create the PDF using the Java Code.

Step 1 : – download the iText.jar.

Step 2 : – use the Following code to Create your First PDF using the Java.


import java.io.FileOutputStream;

import com.itextpdf.text.Document;

import com.itextpdf.text.Font;

import com.itextpdf.text.FontFactory;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfWriter;

public class pdfdemo

{

public static void main(String[] args)

{

try {

//define all the Parameters

Document document = new Document(PageSize.<em>A4</em>, 50, 50, 50, 50);

//now you need to Pass the File name to the Method which will Create that file

PdfWriter <span style="text-decoration: underline;">writer</span> = PdfWriter.<em>getInstance</em>(document, new FileOutputStream("E:\\pdfDemo.pdf"));

//Open the Doc( The File in which u <span style="text-decoration: underline;">ll</span> Write )

document.open();

//Provide the things that u <span style="text-decoration: underline;">wanna</span> Write

document.add(new Paragraph("This is the First Page of ma PDF Creation Demo"));

//You can also change the size <span style="text-decoration: underline;">nd</span> the Font as well as the Color of the Text u <span style="text-decoration: underline;">ll</span> be <span style="text-decoration: underline;">Writting</span>

FontFactory.<em>getFont</em>(FontFactory.<em>COURIER</em>, 25, Font.<em>BOLD</em>);

document.add(new Paragraph("Some more text on the first page with different color and font type."));

//Display on the Console for <span style="text-decoration: underline;">ur</span> Reference to make sure that the File has been Written

System.<em>out</em>.println("File Written E:\\pdfDemo.pdf");

//Make sure that u have Written the Document.Close() Before u execute the Program

document.close();

}catch(Exception e){}

}

}

You gonna get the Following Output :-

pdfDemo
pdfDemo

You can also Use many different thing with this as per your need so Enjoy the Functionalities of Java and innovate something new…

Provide your comments or feedback about this Article.

Thank You & Regards Hitesh Parmar 🙂

Learn, Innovate and Share 🙂

Advertisements

7 thoughts on “Create PDF using Java

  1. hi Hitesh,

    I am using your code.
    It’s working when i was remove all the html tags.
    otherwise it gives some error. It can;t recognize those tags.

    Will you give some idea about this how it will working with those tags.
    It’s urgent
    Please help.
    Thanks in Advance…

    1. ya thanks for reading the post, and ya actually those tags are not needed but when i used that code in this post its automatically been added in to my post so that its there..! so not a big issue just remove those tags and have a great time ..! good luck. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s