iText - Adding a Paragraph


Advertisements

In this chapter, we will see how to create a PDF document and add a paragraph to it using the iText library.

Creating a Paragraph

You can create an empty PDF Document by instantiating the Document class. While instantiating this class, you need to pass a PdfDocument object as a parameter, to its constructor. Then, to add a paragraph to the document, you need to instantiate the Paragraph class and add this object to the document using the add() method.

Following are the steps to create a PDF document with a paragraph in it.

Step 1: Creating a PdfWriter object

The PdfWriter class represents the Doc Writer for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.

Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below.

// Creating a PdfWriter 
String dest = "C:/itextExamples/addingParagraph.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

When the object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified.

Step 2: Creating a PdfDocument

The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor.

Instantiate the PdfDocument class by passing the above created PdfWriter object to its constructor, as shown below.

// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 

Once a PdfDocument object is created, you can add various elements like page, font, file attachment, and event handler using the respective methods provided by its class.

Step 3: Creating the Document class

The Document class of the package com.itextpdf.layout is the root element. While creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.

Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps as shown below.

// Creating a Document   
Document document = new Document(pdfDoc); 

Step 4: Creating a Paragraph object

The Paragraph class represents a self-contained block of textual and graphical information. It belongs to the package com.itextpdf.layout.element.

Instantiate the Paragraph class by passing the text content as a string to its constructor, as shown below.

String para = "Welcome to Howcodex."; 
// Creating an Area Break    
Paragraph para = new Paragraph (para); 

Step 5: Adding Paragraph

Add the Paragraph object created in the previous step using the add() method of the Document class, as shown below.

// Adding area break to the PDF 
document.add(para); 

Step 6: Closing the Document

Close the document using the close() method of the Document class, as shown below.

// Closing the document 
document.close();

Example

The following Java program demonstrates how to create a PDF document and add a paragraph to it using the iText library. It creates a PDF document with the name addingParagraph.pdf, adds a paragraph to it, and saves it in the path C:/itextExamples/.

Save this code in a file with the name AddingParagraph.java.

import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Paragraph;  

public class AddingParagraph {    
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/addingParagraph.pdf";       
      PdfWriter writer = new PdfWriter(dest);           
      
      // Creating a PdfDocument       
      PdfDocument pdf = new PdfDocument(writer);              
      
      // Creating a Document        
      Document document = new Document(pdf);              
      String para1 = "Howcodex originated from the idea that there exists 
      a class of readers who respond better to online content and prefer to learn 
      new skills at their own pace from the comforts of their drawing rooms.";  
      
      String para2 = "The journey commenced with a single tutorial on HTML in 2006 
      and elated by the response it generated, we worked our way to adding fresh 
      tutorials to our repository which now proudly flaunts a wealth of tutorials 
      and allied articles on topics ranging from programming languages to web designing 
      to academics and much more.";              
      
      // Creating Paragraphs       
      Paragraph paragraph1 = new Paragraph(para1);             
      Paragraph paragraph2 = new Paragraph(para2);              
      
      // Adding paragraphs to document       
      document.add(paragraph1);       
      document.add(paragraph2);           
      
      // Closing the document       
      document.close();             
      System.out.println("Paragraph added");    
   } 
}  

Compile and execute the saved Java file from the Command prompt using the following commands −

javac AddingParagraph.java 
java AddingParagraph

Upon execution, the above program creates a PDF document, displaying the following message.

Paragraph added

If you verify the specified path, you can find the created PDF document, as shown below.

Adding Paragraph
Advertisements