【问题标题】:Create a PDF according to a given format using the iText library使用 iText 库根据给定格式创建 PDF
【发布时间】:2017-09-17 11:58:41
【问题描述】:

我正在用 java 做一个小项目,我想从数据库中获取内容并将它们写入 PDF 文件。

我尝试谷歌搜索并想出了iText Library

任何人都可以指导创建一个看起来像所附图像

PDF

PS:我对 JAVA 很陌生。这是我的第一个 java 项目。

【问题讨论】:

  • 这个问题对于 Stack Overflow 来说太宽泛了。从阅读the documentation 开始(如果向下滚动,您会看到发票示例)。开始编码,并在遇到特定技术问题时返回 Stack Overflow。 Stack Overflow 不是“为我工作”平台,也不是学习平台。
  • 当然,还有一本专门用于使用 iText 制作发票的完整书籍:developers.itextpdf.com/content/zugferd-future-invoicing
  • @BrunoLowagie 感谢您的反馈 :) 我只是在寻找入门参考..
  • 这里有很多参考资料:developers.itextpdf.com/books
  • 你有解决办法吗?

标签: java pdf itext file-management


【解决方案1】:

我已经快速实现了您的大部分用例。

代码如下:
首先,我们定义一个小类,作为发票中的一条记录。

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

然后我为发票中的每个大块创建了一个方法。
以标题开头:

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

然后添加标题下方的一小段文字:

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

main 方法如下所示:

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

【讨论】:

  • 你好你能提供一个我无法实现的例子
  • 我的帖子里有一个完整的例子
  • 您好,您能否提供更多信息,我尝试了您的示例,但无法成功。我找不到库和许多其他错误,如果您可以在我生成 pdf 以通过电子邮件发送此 pdf 后为我提供一个示例,我将不胜感激……因为我被困住了,我正在使用 spring 和 java 8 @Joris
  • 您遇到了什么错误?该示例应按原样运行。
  • 是的,但他们中的大多数人都不知道 PdfDocument,例如,表格我也尝试过导入所有可能的机会,但它们都不支持 addCell()
猜你喜欢
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多