pdf system

In this section, you will read about pdf system with an example.

pdf system


In this example we are going to see how the data which we have in the pdf file will be printed on the command prompt. After going through this example you will be able to write the pdf data on the command prompt, just go through this tutorial properly. 

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 helloSystemPDF. 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 Paragraph. Now 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. Inside the constructor of the Document pass the PageSize

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. In this program we are strictly specifying where the output has to be printed. We are printing the output on the command prompt. 

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 pass any  String.  To set the margins of the pdf file call the method setMargins and add it to Document object. The method setMarginMirroring(true), the parameter passed in the method allows you to margin mirroring. Now add the Paragraph object to the previously created 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. 

To run this program firstly compile the program by using javac. The output of the file will be given by the java command. 

The code of the program is given below:

import java.io.*;
import java.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class helloSystemPDF{
public static void main(String arg[])throws Exception
System.out.println("Hello RoseIndia");
Paragraph paragraph=new Paragraph();  
Document document = new Document(PageSize.A4, 3672108180);
new FileOutputStream("helloSystemPDF.pdf"));
document.add(new Paragraph("Margin--->>roseinia.net"));
document.add(new Paragraph("You can visit 
roseindia.net for more java tutorials"));
paragraph.add("Hello roseindia.net,
Hello roseindia.net,Hello roseindia.net,
Hello roseindia.net,Hello roseindia.net,
Hello roseindia.net,Hello 
roseindia.net,Hello roseindia.net.");  
document.add(new Paragraph("Starting on 
the next page, the margins will be mirrored."));  

The output of the program is given below:

Download this example.