我目前正在尝试构建一个小型webtool,并尝试使用DataExporter中的preProcessor(Primefaces)为PDF添加徽标。我已经设置了一个存储徽标的服务器。
现在,当我尝试将DataTable导出为PDFI时,不断收到此错误消息:
com.lowagie.text.Document无法转换为com.itextpdf.text.Document
这是包含preProcessor方法的bean
public void preProcessPDF (Object document) throws IOException, BadElementException, DocumentException
{
Document pdf = (Document) document;
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String logo = servletContext.getRealPath("") + File.separator + "http:" + File.separator + File.separator +"192.168.1.34:8080" + File.separator
+ "Bilder" + File.separator + "ubs.jpg";
pdf.add(Image.getInstance(logo));
}
这是xhtml文件:
<h:commandButton value="Als PDF exportieren">
<!-- <h:outputText value="Als PDF exportieren" />-->
<p:dataExporter type="pdf" target="modulbewertung"
fileName="zusammenfassung_modulbewertung"
preProcessor="#{handleEvents.preProcessPDF}"/>
</h:commandButton>
答案 0 :(得分:1)
com.lowagie.text.Document无法转换为com.itextpdf.text.Document
此错误消息基本上意味着您尝试将com.lowagie.text.Document
类型的对象强制转换为com.itextpdf.text.Document
类型的对象,而该对象不是该对象的实例所有。这很可能发生在以下行:
Document pdf = (Document) document;
您需要确保import
的{{1}}声明如下:
Document
因此不是另一个。如果您无法导入,请验证您是否使用了正确版本的iText。确保您没有多个不同版本的iText库。
对具体问题 无关,您永远不应该使用import com.lowagie.text.Document;
。而是使用getRealPath()
或getResource()
。另见What does servletcontext.getRealPath("/") mean and when should I use it。此外,您不需要使用getResourceAsStream()
来编写URL。