使用iText库根据给定的格式创建PDF [英] Create a PDF according to a given format using the iText library

查看:148
本文介绍了使用iText库根据给定的格式创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java开发小型项目,我想从数据库中获取内容并将其写入PDF文件.

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.

我尝试使用Google搜索,并提出了 iText库.

I tried to googling and came up with iText Library.

任何人都可以指导创建一个 PDF ,其外观类似于随附的图像

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

PS:我对JAVA还是很陌生,这是我的第一个Java项目.

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

然后添加一个表:

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

主要方法如下:

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();
}

这篇关于使用iText库根据给定的格式创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆