This example demonstrates a few basic features of the iText library. It creates a 2-page PDF document using various fonts, adds a header (containing the page number) and a footer, draws simple graphics (a couple of lines), and adds text both at an absolute position, and sequentially in a natural flow. It also adds a table that shows various table features, and finally, rotated text.

Note that this code uses iText 2, which is no longer being developed but which supports most PDF features one is likely to need. This version is preferable to the newer iText 5 and iText 7 because it uses the business-friendly Lesser GPL (or LGPL) license, whereas iText 5 is published under the less permissive Affero GPL which may require you to obtain (and pay for) a license.

iText 2 can be downloaded from and the javadocs are at

An alternative to iText is Apache PdfBox, the API of which is not as full-featured, though. PdfBox shows how to use it to create a PDF.

Another alternative is LibrePDF, a fork of iText that is licensed under the LGPL and MPL.

import java.awt.Color;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class iTextExample

    public static void main (String[] args)

        // creation of the document with a certain size and certain margins
        // (you can use PageSize.Letter instead of PageSize.A4)
        Document document = new Document(PageSize.A4, 50505050);
            // creation of the different writers
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("iTextExample.pdf"));

            // various fonts
            BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252"false);
            BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252"false);
            BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252"false);
            BaseFont bf_symbol = BaseFont.createFont(BaseFont.SYMBOL, "Cp1252"false);

            // headers and footers must be added before the document is opened
            HeaderFooter footer = new HeaderFooter(
                        new Phrase("This is page: "new Font(bf_courier)), true);

            HeaderFooter header = new HeaderFooter(
                        new Phrase("This is a header without a page number"new Font(bf_symbol)), false);


            int y_line1 = 650;
            int y_line2 = y_line1 - 50;
            int y_line3 = y_line2 - 50;

            // draw a few lines ...
            PdfContentByte cb = writer.getDirectContent();
            cb.moveTo(250, y_line3 - 100);
            cb.lineTo(250, y_line1 + 100);
            cb.moveTo(50, y_line1);
            cb.lineTo(400, y_line1);
            cb.moveTo(50, y_line2);
            cb.lineTo(400, y_line2);
            cb.moveTo(50, y_line3);
            cb.lineTo(400, y_line3);
            // ... and some text that is aligned in various ways
            cb.setFontAndSize(bf_helv, 12);
            String text = "Sample text for alignment";
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center"250, y_line1, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right"250, y_line2, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left"250, y_line3, 0);

            // start second page

            // add text in three paragraphs from top to bottom with various font styles
            Paragraph par = new Paragraph("bold paragraph");
            par = new Paragraph("italic paragraph");
            par = new Paragraph("underlined and strike-through paragraph");
            par.getFont().setStyle(Font.UNDERLINE | Font.STRIKETHRU);

            // demonstrate some table features
            Table table = new Table(3);
                // 2 pixel wide blue border
            table.setBorderColor(new Color(00255));
            Cell c = new Cell("header");
            c = new Cell("example cell with rowspan 2 and red border");
            c.setBorderColor(new Color(25500));
            c = new Cell("align center");
            cell = new Cell("big cell");
            c = new Cell("align right");

            // add text at an absolute position
            cb.setFontAndSize(bf_times, 14);
            cb.showText("Text at position 100, 300.");

            // rotated text at an absolute position
            PdfTemplate template = cb.createTemplate(300300);                
            template.setFontAndSize(bf_times, 14);
            template.showText("Rotated text at position 400, 200.");

            float rotate = 90;
            float x = 400;
            float y = 200;
            float angle  = (float) (-rotate * (Math.PI / 180));
            float xScale = (floatMath.cos(angle);
            float yScale = (floatMath.cos(angle);
            float xRot   = (float) -Math.sin(angle);
            float yRot   = (floatMath.sin(angle);

            cb.addTemplate(template, xScale, xRot, yRot, yScale, x, y);

            // we're done!

        } catch (Exception ex) {