pdf list
In this program we are going to know how we can make a list 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 listDemo. 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(). Create a numbered List with 30 point field for the numbers. Add the ListItem to the previously created list. Now add a separator. Create a symboled List with a 30 point field for the symbols. Again add the ListItem to the previously created list. At last add the list to the 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.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class listDemo{ public static void main(String a[])throws Exception { Document doc=new Document(); PdfWriter.getInstance(doc,new FileOutputStream("listDemo.pdf")); doc.open(); //Create a numbered List with 30-point field for the numbers. List list=new List(true,30); list.add(new ListItem("First List")); list.add(new ListItem("Second List")); list.add(new ListItem("Third List")); doc.add(list); //Add a separator. doc.add(Chunk.NEWLINE); //Create a symboled List with a 30-point field for the symbols. list=new List(false,30); list.add (new ListItem ("Orange")); list.add (new ListItem ("Apple")); list.add (new ListItem ("Cherry")); list.add (new ListItem ("Banana")); // Add the list to the document. doc.add (list); doc.close (); } } |
The output of the program is given below: