Home Java Itext pdf Table title
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

pdf Table title

Advertisement
This section teaches you about pdf table title in java.

pdf Table title

     

In this program we are going to tell you how the title will be given to the table of the pdf file. Suppose we have one pdf file in which we have a table and we want to give a title to it. We can make a table and give it a title irrespective of the fact whether the file exists or not. If it exists then its fine, otherwise a pdf file will be created. 

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 tableTitlePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. The name which we have given to the file indicates what it 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(). Make a object of PdfPTable class. It will create a table with 2 rows and 2 columns as we have passed 2 inside the constructor of a PdfPTable. Now make a object of PdfPCell.  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 pass the String . Add the column space, horizontal alignment, background color in the cell. After that add the content to the table. Now add the table to the Document object. 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.*;
import com.lowagie.text.pdf.*;
import java.awt.*;
public class tableTitlePDF{     
public static void main(String arg[])throws Exception{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("tableTilePDF.pdf"));
document.open();
PdfPTable table=new PdfPTable(2);
PdfPCell cell = new PdfPCell (new Paragraph ("Rose India"));
cell.setColspan (2);
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (new Color (128, 200, 128));
cell.setPadding (10.0f);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Name"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (new Color (255, 200, 0));
cell.setPadding (10.0f);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Place"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (new Color (255, 200, 0));
cell.setPadding (10.0f);
table.addCell (cell);
table.addCell("NewsTrack");
table.addCell("Delhi");
table.addCell("RoseIndia");
table.addCell("Delhi");
document.add(table);
document.close();
}
}

The output of the program is given below:

Download this program

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: pdf Table title   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Giovanni Lamia
October 26, 2011
Cell and Table and Pages

What happen when there are many row and I want for every page the title for every cell??
DMCA.com