iText - Setting Font



In this chapter, we will see how to set color and font to text in a PDF document using the iText library.

Setting Font of the Text in a PDF

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.

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. You can set color and font to the text using the methods setFontColor() and setFont() respectively.

Following are the steps to set color and font to text in a pdf document.

Step 1: Creating a PdfWriter object

The PdfWriter class represents the DocWriter 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/fonts.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

When an 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 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 Text

Create the text by instantiating the Text class of the package com.itextpdf.layout.element as shown below.

// Creating text object 
Text text = new Text("Tutorialspoint"); 

Step 5: Setting the font and color to the text

Create the PdfFont object using the createFont() method of the class PdfFontFactory of the package com.itextpdf.kernel.font as shown below

// Setting font of the text PdfFont 
font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD); 

Now, set font to the text using the setFont() method of the Text class to this method. Pass the PdfFont object as a parameter, as shown below.

text1.setFont(font);

To set the color to the text invoke the setFontColor() method of the Text class, as shown below.

// Setting font color 
text.setFontColor(Color.GREEN); 

Step 6: Adding text to the paragraph

Create a Paragraph class object and add the above created text using its add() method, as shown below.

// Creating Paragraph 
Paragraph paragraph = new Paragraph();  

// Adding text to the paragraph 
paragraph.add(text); 

Step 7: Adding paragraph to the document

Add the paragraph to the document using the add() method of the Document class, as shown below.

doc.add(paragraph1)

Step 8: 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 set color and font to text in a PDF using the iText library. It creates a PDF document with the name fonts.pdf, formats the text, and saves it in the path C:/itextExamples/

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

import com.itextpdf.io.font.FontConstants; 
import com.itextpdf.kernel.color.Color; 
import com.itextpdf.kernel.font.PdfFontFactory; 
import com.itextpdf.kernel.font.PdfFont; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Paragraph; 
import com.itextpdf.layout.element.Text;  

public class FormatingTheText {     
   public static void main(String args[]) throws Exception {        
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/fonts.pdf";   
      PdfWriter writer = new PdfWriter(dest);             
   
      // Creating a PdfDocument object      
      PdfDocument pdf = new PdfDocument(writer);                   
   
      // Creating a Document object       
      Document doc = new Document(pdf);
   
      // Creating text object       
      Text text1 = new Text("Tutorialspoint");              
   
      // Setting font of the text       
      PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);       
      text1.setFont(font);                 
   
      // Setting font color
      text1.setFontColor(Color.GREEN);
   
      // Creating text object
      Text text2 = new Text("Simply Easy Learning");
      text2.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));         
      
      // Setting font color
      text2.setFontColor(Color.BLUE);
      
      // Creating Paragraph
      Paragraph paragraph1 = new Paragraph();
      
      // Adding text1 to the paragraph
      paragraph1.add(text1);
      paragraph1.add(text2);
      
      // Adding paragraphs to the document
      doc.add(paragraph1);
      doc.close();       
      
      System.out.println("Text added to pdf ..");   
   }     
}   

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

javac FormatingTheText.java 
java FormatingTheText 

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

Text added to pdf ..

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

Fonts
Advertisements