java iText font

The Font class object is used to specify the font style of the text. It is represented by com.itextpdf.text.Font class.


1. Create Document instance. It represents the current document to which we are adding content. 2. Create OutputStream instance. It represents the generated pdf. 3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor. 4. Open the Document by calling 5. Create font object and specify the style. 6. Add the content to the document by calling document.add() method using chunk, paragraph etc object with font object. 7. Close the document by calling document.close() method.


import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 * This class is used to create a pdf file using iText jar.
 * @author w3spoint
public class PDFFontExample {
  public static void main(String args[]){
    try {
	//Create Document instance.
	Document document = new Document();
	//Create OutputStream instance.
	OutputStream outputStream = 
	    new FileOutputStream(new File("D:\\TestFontFile.pdf"));
	//Create PDFWriter instance.
        PdfWriter.getInstance(document, outputStream);
        //Open the document.;
        //Create Font objects
        Font font = new Font(Font.FontFamily.TIMES_ROMAN, 20,
                      Font.ITALIC | Font.UNDERLINE | Font.BOLD);
        //Add content to the document using Paragraph, Chunk etc 
        //objects with font object.
        document.add(new Paragraph("Test font in pdf file.", font));
        //Close document and outputStream.
        System.out.println("Pdf created successfully.");
    } catch (Exception e) {


Pdf created successfully.

Download this example.   Next Topic: Java iText chapter and section. Previous Topic: Java iText image.


Content Protection by