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

≯℡__Kan透↙ 提交于 2020-04-10 04:09:28

问题


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

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


回答1:


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)+""));
    }

    layoutDocument.add(table);
}

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
    addTitle(layoutDocument);

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

    // articles
    layoutDocument.close();
}


来源:https://stackoverflow.com/questions/46263806/create-a-pdf-according-to-a-given-format-using-the-itext-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!