Write Hello in the pdf file

Here, you will see a example that writes hellow in the pdf file.

Write Hello in the pdf file


In this program we are going to tell you how you can write a "Hello" word in a pdf file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named HelloWordPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specific OutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph we have passed one String "Hello Pdf" . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.*;
 public class HelloWordPDF
    public static void main(String arg[])throws Exception
       Document document=new Document();
       PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
      document.add(new Paragraph("Hello Pdf"));

The output of the program is given below:

Download this example.


Share on Google+Share on Google+

Write Hello in the pdf file

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Write Hello in the pdf file   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 19, 2011
Itext in JSP

Hi, how do you call the java bean method that handles the creation of pdf using jsp? I have this code: <jsp:useBean id="create" class="itext.HelloWordPDF"/> <jsp:setProperty name="create" property="*"/> Thanks!
June 27, 2011
feedback;select * from tab;

Vishal Nayakwadi
September 15, 2011
How to convert a java swing form to PDF file

Hello, I want to know about how to convert java swing form to PDF file.
September 4, 2012
Such a mediocre example

This example is as good as not reading it. What is there in this? Some more concepts can be illustrated. You are just filling pages in website. Thats it.
October 17, 2012
Not worked for me

Its not working and not showing any error either.
November 25, 2012
Pdf Generator Hello World Program

The best of all. Awesome example and explanation. Thanks a ton.
September 25, 2013
Error in packages

I am getting follwing Error when I run sample code package com.lowagie.text.pdf does not exist where do I get this package or how can I import them. please suggest