Search code examples
javapdfitextacrofields

How to export data set using pdf template with itext?


In my project, some data sets are needed to be exported in PDF format.

I learned that iText is helpful, and PdfpTable can do the work, but it needs much code to deal with styles. While using PDF template can save time and code for adjusting style, but I can only set certain fields left in the template.

Can you give me some suggestions to show the data sets using commands like foreach? Thanks in advance!

Here are my code using pdfpTable, which has done the work, but the code is a little ugly:

 PdfPTable pdfTable = createNewPDFTable();    
   for (int i = 0; i < dataSet.size(); i++) {
                MetaObject metaObject = SystemMetaObject.forObject(dataSet.get(i));
                for (String field : fields) {
                    Phrase phrase = new Phrase(String.valueOf(metaObject.getValue(field) != null ? metaObject.getValue(field) : "")
                            , PDFUtil.createChineseSong(DEFAULT_CELL_FONT_SIZE));
                    PdfPCell fieldCell = new PdfPCell(phrase);
                    fieldCell.setBorder(Rectangle.NO_BORDER);
                    fieldCell.setFixedHeight(DEFAULT_COLUMN_HEIGHT);
                    fieldCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    fieldCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    pdfTable.addCell(fieldCell);
                }
            }

Here are some code using pdfp template,which is copied from itext examples, the work is unfinished yet because i haven't find a proper way to show the data set.

PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        form.setField("text_1", "Bruno Lowagie");
        form.setFieldProperty("text_1", "setfflags", PdfFormField.FF_READ_ONLY, null);

Solution

  • There is an inconsistency in your question. You write: PdfpTable can do the work, but it needs much code to deal with styles. However, in your first code snippet, you don't really create your PDFs the way one would expect. Instead of producing a high volume of finished PDFs, you create use PdfPTable to create a template. I assume you then use that template to create a high volume of finished PDFs.

    If you want to use a template and populate it afterwards, you shouldn't create your form using iText. Create it manually, for instance using Open Office or Libre Office. See for instance the example in chapter 6 of my book (section 6.3.5). Create the template with a tool that has a GUI, then fill out that template many times using iText.

    This approach has some down-sides: all the content has to fit the fields you define. All fields have a fixed position on a fixed page.

    If "applying styles through code" is a problem, you may want to follow the approach described in the ZUGFeRD book. In that book, we create HTML first: Creating HTML invoices.

    Once you have the HTML, then convert the HTML to PDF, and use CSS to apply styles: Creating PDF invoices.

    This is how we create a ZUGFeRDDocument:

    ZugferdDocument pdfDocument = new ZugferdDocument(
        new PdfWriter(fos), ZugferdConformanceLevel.ZUGFeRDComfort,
        new PdfOutputIntent("Custom", "", "http://www.color.org",
            "sRGB IEC61966-2.1", new FileInputStream(INTENT)));
    pdfDocument.addFileAttachment(
        "ZUGFeRD invoice", dom.toXML(), "ZUGFeRD-invoice.xml",
        PdfName.ApplicationXml, new PdfDictionary(), PdfName.Alternative);
    pdfDocument.setTagged();
    
    HtmlConverter.convertToPdf(
        new ByteArrayInputStream(html), pdfDocument, getProperties());
    

    The getProperties() method looks like this:

    public ConverterProperties getProperties() {
        if (properties == null) {
            properties = new ConverterProperties()
                .setBaseUri("resources/zugferd/");
        }
        return properties;
    }
    

    You can find other examples on how to use HTML to PDF here: pdfHTML add-on (read the introduction).

    Note that you are using an old version of iText. The examples I shared are using iText 7. There's a huge difference between iText 5 and iText 7.