How to write hello world by using Multiple Writers in pdf

Here, you will learn how to write hello world by using multiple writers in pdf.

How to write hello world by using Multiple Writers in pdf


In this program we are going to tell you how you can write a simple "Hello World" word in a pdf, rtf and html 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* 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. The package com.lowagie.text.html.HtmlWriter will help you to write the content in Html file, The package com.lowagie.text.pdf.PdfWriter helps to write the content in the pdf file and com.lowagie.text.rtf.RtfWriter2 helps you to write in rtf format. 

Now create a file named differentWritersMultiple. 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. In our example we have created a three different writers that listen to the document. The different writers are for pdf, html and for rtf. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by After that add the paragraph to the document. For that 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 World" . Now we need to make the references,  we add the references, but only  to the HTML page. 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 com.lowagie.text.Anchor;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

public class differentWritersMultiple {

  public static void main(String[] args) {

  System.out.println("Hello World in PDF, RTF and HTML");

  Document document = new Document();
  try {
  PdfWriter pdf = PdfWriter.getInstance(document,
  new FileOutputStream("differentWritersdPdf.pdf"));
  RtfWriter2 rtf = RtfWriter2.getInstance(document,
  new FileOutputStream("differentWritersRtf.rtf"));
  HtmlWriter html = HtmlWriter.getInstance(document,
  new FileOutputStream("differentWritersdHtml.html"));;
  document.add(new Paragraph("Hello World"));
  Anchor pdfRef = new Anchor("see Hello World in PDF.");
  Anchor rtfRef = new Anchor("see Hello World in RTF.");
  catch (DocumentException de) {
  catch (IOException ioe) {


The output of  the program is given below:

Download this program.




We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

How to write hello world by using Multiple Writers in pdf

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



Discuss: How to write hello world by using Multiple Writers in pdf   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image