Home » Java » Create a PDF according to a given format using the iText library

Create a PDF according to a given format using the iText library

Posted by: admin December 28, 2021 Leave a comment


I’m working on small project in java, there I want to fetch the contents from a database and write them into a PDF file.

I tried to googling and came up with iText Library.

Can anyone guide to create a PDF that looks like the enclosed image computer generated invoice

PS: I’m pretty new to JAVA.and it’s my first java project.


I’ve done a quick implementation of most of your use-case.

Here’s the code:
First we define a small class that acts as a single record in the invoice.

static class Article{
    int SNO;
    String description;
    int quantity;
    double unitPrice;
    public Article(int SNO, String description, int quantity, double unitPrice)
        this.SNO = SNO;
        this.description = description;
        this.quantity = quantity;
        this.unitPrice = unitPrice;

Then I’ve created a method for each of the big blocks in the invoice.
Starting with the title:

public static void addTitle(Document layoutDocument)
    layoutDocument.add(new Paragraph("RETAIL INVOICE").setBold().setUnderline().setTextAlignment(TextAlignment.CENTER));

Then adding the little paragraph of text that’s underneath the title:

public static void addCustomerReference(Document layoutDocument)
    layoutDocument.add(new Paragraph("M/s Indian Convent School").setTextAlignment(TextAlignment.LEFT).setMultipliedLeading(0.2f));
    layoutDocument.add(new Paragraph("y Pocket-3, Sector-24, Rohini Delhi-110085").setMultipliedLeading(.2f));
    layoutDocument.add(new Paragraph("b 011-64660271").setMultipliedLeading(.2f));

And then adding a table:

public void addTable(Document layoutDocument, List<Article> articleList)
    Table table = new Table(UnitValue.createPointArray(new float[]{60f, 180f, 50f, 80f, 110f}));

    // headers
    table.addCell(new Paragraph("S.N.O.").setBold());
    table.addCell(new Paragraph("PARTICULARS").setBold());
    table.addCell(new Paragraph("QTY").setBold());
    table.addCell(new Paragraph("RATE").setBold());
    table.addCell(new Paragraph("AMOUNT IN RS.").setBold());

    // items
    for(Article a : articleList)
        table.addCell(new Paragraph(a.SNO+""));
        table.addCell(new Paragraph(a.description));
        table.addCell(new Paragraph(a.quantity+""));
        table.addCell(new Paragraph(a.unitPrice+""));
        table.addCell(new Paragraph((a.quantity * a.unitPrice)+""));


The main method then looks like this:

public static void main(String[] args) throws FileNotFoundException {

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter("MyFirstInvoice.pdf"));
    Document layoutDocument = new Document(pdfDocument);

    // title

    // customer reference information
    addTable(layoutDocument, Arrays.asList(
            new Article(1, "Envelopes",2000, 1.70),
            new Article(2, "Voucher Book", 50, 41)));

    // articles