2016-08-15 135 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");