Anil Pradhan
Anil Pradhan

Reputation: 105

Create form data in PDF Java

I want to extract one object's details to a PDF file and also wants to have a facility within that exported PDF file to amend/edit the data.

For eg: Try to export student object details to PDF in some very specific format like display name in first row, age in second row etc and once the data is exported I should be able to edit it too ( like changing the retrieved age).

Initially I was thinking of creating a GUI within this application which shows the data and user can edit before performing any action like printing, mailing etc, but wanted to avoid this GUI creation and directly deal with the exported PDF there.

I'm very new to this area and tried googling about it, but couldn't find one correct solution.

The object in the question is a java object and I'm trying to view this java object details in PDF file.

Any pointers would greatly help me.

Upvotes: 2

Views: 2760

Answers (3)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Download Chapter6 of my book and read the part about creating a form using OpenOffice (or LibreOffice) in section 6.3.5. This is the template in which you define fields for the student's "name", 'age", etc...

Once you have this template with the fields "name" and "age", you can use iText to fill out this form like this:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader,
            new FileOutputStream(dest));
    AcroFields fields = stamper.getAcroFields();
    fields.setField("student", "Anil Pradhan");
    fields.setField("age", "25");
    stamper.close();
    reader.close();
}

In this example, src is the path to your empty template and dest is the path to a newly created form that is filled out.

If you have dest, you can also programmatically retrieve the content of the fields. For instance:

PdfReader reader = new PdfReader(src);
AcroFields fields = reader.getAcroFields();
String name = fields.getField("name");
String age = fields.getField("age");

It's as simple as this. You can find more examples here and be sure to download the free ebook The Best iText Questions on StackOverflow to learn more about other options.

Upvotes: 1

plinth
plinth

Reputation: 49199

What it sounds like you're trying to do is to serialize and deserialize a Java object into a PDF Form. This is straightforward, but it isn't a small amount of work.

Ignoring XFA, in PDF land a Form is a hierarchical tree of fields. The nodes in the tree have names and may also have values that will be projected downward (such as field type or default value). The leaves are all represented by widget annotations.

The main field types in PDF are:

  • Text
  • Button
  • Choice
  • Signature

From there is straight forward to map the following types:

  • Numeric
  • Boolean
  • String
  • Enum
  • EnumSet

It is possible to do more complex types like Date, but there is no native support and you would need to depend on undocumented js libraries from Adobe.

If your type includes no more than this, then it's doable. Both iText and JoltImage can work as a back end for this, but you'll need to have a fairly good understanding of PDF to use iText and at least a rudimentary understand for JoltImage. For example, to make a set of radio buttons in JoltImage, you would do something like this:

    // make a document
    PdfGeneratedDocument doc = new PdfGeneratedDocument();
    // add a form
    doc.setForm(new PdfForm());
    // make a page
    PdfGeneratedPage page = doc.addPage(PdfDefaultPages.letter());
    // create a font resource
    String font = doc.getResources().getFonts().addFromFontName("Arial");

    String[] values = new String[] { "Yes", "No", "Undecided" };
    PdfBounds[] bounds = new PdfBounds[] {
        new PdfBounds(72, 700, 12, 12),
        new PdfBounds(72, 680, 12, 12),
        new PdfBounds(72, 660, 12, 12)
    };
    // a RadioButtonFormField is a hierarchy of a parent field with a child
    // RadioButtonWidgetAnnotation for each choice.
    // This call, in addition to making all the fields will add the
    // annotations to the page supplied.
    RadioButtonFormField ff = RadioButtonFormField.makeRadioSet(doc.getResources(), page, "Choice", values[0], values[0],
        values, bounds);

    // Add the parent field to the form tree
    doc.getForm().getFields().add(ff);

    // add labels onto the page.
    // labels are not typically part of the widget annotation.
    for (int i = 0; i < values.length; i++)
    {
        page.getDrawingList().add(new PdfTextLine(font, 12, values[i], new PdfPoint(bounds[i].getRight() + 4, bounds[i].getBottom())));
    }
    doc.save("radiobuttons.pdf");

Upvotes: 1

FraK
FraK

Reputation: 1089

Have you looked at Jasper Reports:

Jasper Reports Library

With this library you can define the template of the PDF, you can decide what and where you should put all the information you want. I have used it and it is quite good generating reports.

Upvotes: 0

Related Questions