Itext PDF- Header – Add title and page number


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ItextMain {

    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/bala/itextheader.pdf"));
        PdfHeader event = new PdfHeader();
        writer.setPageEvent(event);
        document.open();
        document.addAuthor("Balachandar");
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        document.add(new Paragraph("Hello World!"));
        document.newPage();
        document.add(new Paragraph("Hello World!"));
        document.close();
    }

    public static class PdfHeader extends PdfPageEventHelper {

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            try {
                Rectangle pageSize = document.getPageSize();
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Title"), pageSize.getLeft(275), pageSize.getTop(30), 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase(String.format("No:%s", String.valueOf(writer.getCurrentPageNumber()))),
                        pageSize.getRight(30), pageSize.getTop(30), 0);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

Adding SmallCap style in PDF with Itext API

Itext API does not support smallcaps styling by default. So if we want to get this style in pdf, then we should use the AlegreyaSansSC-Light.ttf from Google fonts

The link is https://www.google.com/fonts/specimen/Alegreya+Sans+SC

Please refer the below code on how to use this new font in the code


import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class PDFMain {

    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("/home/bala/itext.pdf"));
        document.open();
        document.addAuthor("Balachandar");
        Paragraph paragraph1 = new Paragraph();
        paragraph1.add(new Chunk("Test with Open sans font"));
        paragraph1.setFont(AppFontFactory.getOpenSansFont());
        Paragraph paragraph2 = new Paragraph();
        paragraph2.add(new Chunk("Test with Normal sans font"));
        Paragraph paragraph3 = new Paragraph();
        Chunk chunk = new Chunk("test small");
        chunk.setFont(AppFontFactory.getSmallCapsPdfFont());
        paragraph3.add(chunk);
        document.add(paragraph1);
        document.add(paragraph2);
        document.add(paragraph3);
        document.close();
    }

    public static class AppFontFactory {

        //Assume that the "customfonts" is folder which contains all the fonts you want to use it
        static {
            FontFactory.register("customfonts/OpenSans-Regular.ttf", "opensans");
            FontFactory.register("customfonts/AlegreyaSansSC-Light.ttf", "alegreyasans");
        }
        public static Font getSmallCapsPdfFont() {
            return FontFactory.getFont("alegreyasans", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10);
        }
        public static Font getOpenSansFont() {
            return FontFactory.getFont("opensans", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10);
        }
    }

}

How to use Google Fonts in Itext API

For one of my recent project, there is an export to PDF functionality. So the application should generate the pdf on the fly and render it on the browser when the user clicks on export link. We use Itext API to generate the PDF

As we use lower version of the Itext API, We don’t have much support for the new fonts. But Itext provides a way to add the new fonts with the help of FontFactory class.

We can get open source fonts from Google[https://www.google.com/fonts/] and use it in our code. Sample code is given below


import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class PDFMain {

    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("/home/bala/itext.pdf"));
        document.open();
        document.addAuthor("Balachandar");
        Paragraph paragraph1 = new Paragraph();
        paragraph1.add(new Chunk("Test with Open sans font"));
        paragraph1.setFont(AppFontFactory.getOpenSansFont());
        Paragraph paragraph2 = new Paragraph();
        paragraph2.add(new Chunk("Test with Normal sans font"));
        document.add(paragraph1);
        document.add(paragraph2);
        document.close();
    }

    public static class AppFontFactory {

        //Assume that the fonts is folder which contains all the fonts you want to use it
        static {
             FontFactory.register("fonts/OpenSans-Regular.ttf", "OpenSans");
        }
        public static Font getOpenSansFont() {
            return FontFactory.getFont("OpenSans", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 15);
        }
    }
}