Mr Man
Mr Man

Reputation: 1588

Writing dates and int's to an existing .pdf

I have an existing pdf that I am filling out dynamically in java. I have all the fields that require strings set up and working, but the fields that use int or date dont seem to work. Here is the relevant code:

        response.setHeader(contentDisposition, "attachment; filename=" + officialFile);    
        reader = new PdfReader(this.getServletContext().getResource("/pdf/" + officialFile));
        stamp = new PdfStamper(reader, response.getOutputStream());
        form = stamp.getAcroFields();
        // form.setField("date", currentDate);  //This is a date, and will give me an error
        form.setField("last_name", lName);
        form.setField("first_name", fName);
        form.setField("first_middle_last", fmlName);
        form.setField("status", studentStatus);
        // form.setField("HR's", hours); //This is an int, and will give me an error
        // form.setField("CENSUS DATE", censusDate);  //This is a date, and will give me an error
        form.setField("term", term);

        stamp.setFormFlattening(true);
        stamp.close();

So the parts that are commented out are where my int's and dates are supposed to be written on the pdf. I was wondering if there was a way to write them to this pdf without converting them to strings, and if there is no way.....then what would be the best way to convert them? Thanks in advance.

Upvotes: 0

Views: 543

Answers (2)

duffymo
duffymo

Reputation: 308763

If you're using iText, another nice alternative is to use a Velocity template to create an .fo XML and then use an FO engine to generate the PDF. Velocity can format that date using its DateTools, which uses the DateFormat underneath.

Try it if iText doesn't work out for you.

Upvotes: 1

Udo Held
Udo Held

Reputation: 12538

Convert them.

Integer.toString(yourInt);

For the Date use the SimpleDateFormatter according to your requirements.

SimpleDateFormat sf = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate = sf.format(yourDate);

Upvotes: 4

Related Questions