2014-02-18 122 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中,然後看看會發生什麼。 –

相關問題