如何在使用iText创建的pdf中显示XPage中的日期字段

时间:2014-02-18 00:34:56

标签: xpages

在使用iText观看David Leedy's video on creating pdfs from xpages之后 - 我尝试在我们的XPage应用程序中创建表格的pdf。表单基本上是一个包含各种数据字段的2列表 - 名称,地址等。所有文本字段都显示完整但不显示日期字段。这是一个例子,我试图在我的桌子的单元格7中显示人的出生日期(Person_dob)。

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

这会在表格单元格中显示空白,而不是日期字段的值;我已经尝试更改代码并使用getItemValueDate和getItemValue而不是getItemValueString,但这会在它甚至创建之前崩溃。毫无疑问,我错过了一些显而易见的东西但我却看不到它。

1 个答案:

答案 0 :(得分:1)

使用getItemValueDate()将日期作为Date对象获取,并使用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()));
}

如果您希望更好地控制日期格式,则可以改为使用SimpleDateFormat

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)));
}