itext chunk

In this free tutorial we are going to tell you about chunk in iText. iText is a framework for creating pdf files in java.

itext chunk


In this free tutorial we are going to tell you about chunk in iTextiText is a framework for creating pdf files in java. A Chunk is the smallest significant part of the text that can be added to a document. All the contents of the Chunk are of the same font, fontsize, style, color, etc. In the following sections you will know more about the Chunk functionality. 

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. Without this jar file our application will not run. The packages which we need to import are* for input output, java.awt.* package, 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 chunkPDF. 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 for you by that name.

Now open the document by Now make a object of the Chunk class. Use the method setUnderline() which takes two argument. This method will draw a line that has the length of the Chunk, the first parameter points to the thickness which positioned at -2f above the baseline of the Chunk. The method setBackground( lets you define the color of the line. Now add the chunk to the previously created Document. 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.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class chunkPDF{
  public static void main(String arg[])throws Exception{
  Document document=new Document();
  PdfWriter.getInstance(document,new FileOutputStream("chunkPDF.pdf"));;
  Chunk chunk=new Chunk("Welecome To RoseIndia.");
  Chunk chunk1=new Chunk("");

The output of the program is given below:

Download this example.

Share on Google+Share on Google+

itext chunk

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



Discuss: itext chunk   View All Comments

Post your Comment

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