0

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);
刘思凡
  • 423
  • 2
  • 14

1 Answers1

1

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for your reply. Maybe I did not describe the question clearly, the first snippet with PdfpTalbe is what I used currently, it has done the whole work, and I want to use PDF template to replace the first way, but the work is not finished yet because I haven't found a proper way to show the data set. Using HTML is a good suggestion, I'll have a try. – 刘思凡 Jun 26 '17 at 07:33
  • I didn't understand (and I still don't understand) why you are using form fields in your `PdfPTable`. I would just use the `PdfPTable` and populate it with data (not with form fields). – Bruno Lowagie Jun 26 '17 at 07:41
  • I don't use form fields in PdfpTalbe, I'm just wanna to use PDF template and form fileds to replace PdfpTale. – 刘思凡 Jun 26 '17 at 07:48
  • Aha, now I see. I was confused by the name `fieldCell`. – Bruno Lowagie Jun 26 '17 at 07:52