Java iText Format Style Text Example

In this iText tutorial, we will see how to format the text you are adding to the PDF document. Formatting is a basic requirement for any document processor and PDF is no exception. Formatting includes specifying a different font, adding color, character spacing,styling, text size and so on. We will provide some examples on how to achive this formatting using iText with suitable examples. I would strongly recommend the readers to go through our initial tutorials on iText as they are the building blocks for this section. At the end of this tutorial, you will see how iText makes the formatting operation feel so easy!Content Formatting in iText is derived with the help of Font object defined in com.itextpdf.text.Font.

1) To begin with formatting text in PDF, you will have to create a Document Object, PdfWriter Object and open the Document object for styling text.
            Document iText_Formatting_Example = new Document();
            PdfWriter.getInstance(iText_Formatting_Example, new FileOutputStream("Sample.pdf"));
            iText_Formatting_Example.open();

2)We now define a Font Object in iText. We do all the formatting that we need for the PDF document with this Font object.
            Font myContentStyle=new Font(); /* Define a new Font Object */ 

2.1) How do I make a text appear bold in a PDF document using iText? Code example below
            myContentStyle.setStyle("bold");
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold",myContentStyle)); /* Adds content in Bold */

2.2) How do I make a text appear in italic, with font color of Red? We will use both "setStyle" and "setColor" for this case. Code example below
            myContentStyle.setStyle("italic");
            myContentStyle.setColor(255,0,0);
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold Red",myContentStyle)); /* Adds content in Bold+Italic Red*/

Since we are using the same "myContentStyle" object, the "bold" property will also apply to this text.You can define a separate font object if required. The best practice would be to know what kind of styles are required for your PDF document and use your objects accordingly.

2.3) How do I underline a text in Green font using iText? Same approach, example below
            myContentStyle.setStyle("underline");
            myContentStyle.setColor(0,255,0);
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold Green with Underline",myContentStyle)); /* Adds content in Bold+Italic+Underline Green*/

2.4) And, how to I increase my font size in iText? To do this, you have to use the "setSize" method of Font object. Example below
            Font mySizeSpecification=new Font();
            mySizeSpecification.setSize(20);
            iText_Formatting_Example.add(new Paragraph("iText Setting Font Size",mySizeSpecification)); /* Font Size */

What other options are available for setting the style of a font? You can make a font Bold, normal, italic, oblique, underline and line-through..Cool..the basic stuffs you need.

3) Given a style how do we know what styles are incorporated into the PDF text? The Font class has some getter methods that return some boolean output back to us to know if the style has a specific property turned on. Some examples below;

            /* Check if a Font object has Bold, Italic, Underline properties set */
            //System.out.println(myContentStyle.isBold()); /* Is Bold enabled? */
            //System.out.println(myContentStyle.isItalic()); /* Is Italic enabled? */
            //System.out.println(myContentStyle.isUnderlined()); /* Is the text underlined? */
            //System.out.println(mySizeSpecification.getSize()); /* What is the size of the font? */

We will wrap all these together and present a complete Java Code, that explains how to set formatting options in a PDF document using iText. Complete code below
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextFormatting {  
     public static void main(String[] args){
        try {
            Document iText_Formatting_Example = new Document();
            PdfWriter.getInstance(iText_Formatting_Example, new FileOutputStream("Sample.pdf"));
            iText_Formatting_Example.open();
            Font myContentStyle=new Font(); /* Define a new Font Object */
            myContentStyle.setStyle("bold");
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold",myContentStyle)); /* Adds content in Bold */            
            myContentStyle.setStyle("italic");
            myContentStyle.setColor(255,0,0);
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold Red",myContentStyle)); /* Adds content in Bold+Italic Red*/            
            myContentStyle.setStyle("underline");
            myContentStyle.setColor(0,255,0);
            iText_Formatting_Example.add(new Paragraph("iText Formatting Example - Bold Green with Underline",myContentStyle)); /* Adds content in Bold+Italic+Underline Green*/            
            /* Check if a Font object has Bold, Italic, Underline properties set */
            //System.out.println(myContentStyle.isBold()); /* Is Bold enabled? */
            //System.out.println(myContentStyle.isItalic()); /* Is Italic enabled? */
            //System.out.println(myContentStyle.isUnderlined()); /* Is the text underlined? */
            //System.out.println(mySizeSpecification.getSize()); /* What is the size of the font? */
            Font mySizeSpecification=new Font();
            mySizeSpecification.setSize(20);
            iText_Formatting_Example.add(new Paragraph("iText Setting Font Size",mySizeSpecification)); /* Font Size */            
            iText_Formatting_Example.close();        }
        catch (Exception i)        {
            System.out.println(i);
        }
    }
}

When you run this code, you will get a good looking PDF with different styles on the PDF content. This is shown in the screen shot below

Formatting Text Using iText - Java Example

No comments:

Post a Comment