2016-08-15 137 views
0

後恢復primefaces的DocumentViewer我使用的是從primefaces的擴展這樣的DocumentViewer組分:頁面重載

<pe:documentViewer cache="true" value="#{myBean.streamedContent}"/> 

爲myBean是SessionScoped。

如果我重新加載頁面,getter被調用,並且streamedContent不爲空,但查看器顯示一個空白頁面和消息stream must have data

如何在頁面重新加載時在查看器中恢復文檔?

回答

1

我轉載使用PrimeFaces Extensions Showcase您的問題,所以我用下面的代碼

ByteArrayOutputStream out = new ByteArrayOutputStream(); 

Document document = new Document(); 
PdfWriter.getInstance(document, out); 
document.open(); 

for (int i = 0; i < 50; i++) { 
     document.add(new Paragraph("All work and no play makes Jack a dull boy")); 
} 

document.close(); 
content = new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()), "application/pdf"); 

但我已經找到了解決辦法。問題只存在於使用DefaultStreamedContent,但是當我檢查了層次結構後,我發現使用ByteArrayContent也是可行的。即使在頁面重新加載後,它也可以工

使用範例:

content = new ByteArrayContent(out.toByteArray(), "application/pdf");