0
votes

After watching David Leedy's video on creating pdfs from xpages using iText - I tried to create a pdf of a form in one of our XPage apps. The form is basically a 2 column table with various data fields - name, address etc. All of the text fields display perfectly but not the date fields. Here is one example where I am trying to display the date of birth of the person (Person_dob) in cell 7 of my table.

var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(currdoc.getItemValueString("Person_dob")));

This displays a blank in the table cell, not the value of the date field; I have tried changing the code and used both getItemValueDate and getItemValue instead of getItemValueString but this crashes the pdf before it even creates. No doubt I am missing something obvious but I just cannot see it.

1

1 Answers

1
votes

Use getItemValueDate() to get date as Date object and convert it to a string in locale date format with toLocaleDateString():

var date = currdoc.getItemValueDate("Person_dob");
if (date != null) {
    var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(date.toLocaleDateString()));
}

If you wish more control over the date format then you can use SimpleDateFormat in instead:

var date = currdoc.getItemValueDate("Person_dob");
if (date != null) {
    var datePattern = new java.text.SimpleDateFormat("yyyy-MM-dd");
    var cell7 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(datePattern.format(date)));
}