2014-02-18 125 views
0

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

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

这会在表单元格中显示一个空白,而不是日期字段的值;我试图改变代码,并使用getItemValueDate和getItemValue而不是getItemValueString,但是它甚至在创建之前崩溃了pdf。毫无疑问,我失去了一些明显的东西,但我看不到它。

回答

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

SimpleDateFormat是你的朋友! – stwissel

+0

感谢您的帮助;我已经尝试了两种建议,但在这两种情况下,我都收到一条错误消息“Firefox无法在http://xyz.nsf/pdf_test.xsp?action = openDocument&documentId = C64E45A60AB1014A80257C0C00607BAB中找到该文件。 – user3321245

+0

它没有日期字段真的工作,不只是从上面添加这些行吗?尝试将日期代码行放在'try {'... code ...'catch(e){'... print error ...'}'block中,然后看看会发生什么。 –

相关问题