我正在尝试向 pdf 文件添加徽标,但出现错误消息 "com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document"
Im trying to add a logo to a pdf file but theres an error message "com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document"
我目前正在尝试构建一个小网络工具,并尝试使用 DataExporter (Primefaces) 中的预处理器向 PDF 添加徽标。我已经设置了一个存储徽标的服务器。
现在,当我尝试将 DataTable 导出到 PDF 时,我不断收到此错误消息:
com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document
这就是包含预处理器方法的 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>
com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document
此错误消息基本上意味着您正在尝试将类型 com.lowagie.text.Document
的对象转换为类型 com.itextpdf.text.Document
的对象,而该对象根本不是该对象的实例。这很可能发生在以下行中:
Document pdf = (Document) document;
您需要确保 Document
的 import
声明如下所示:
import com.lowagie.text.Document;
因此不是另一个。如果无法导入,请验证您使用的 iText 版本是否正确。确保您也没有多个不同版本的 iText 库。
与具体问题无关,永远不要使用getRealPath()
。而是使用 getResource()
或 getResourceAsStream()
。另见 What does servletcontext.getRealPath("/") mean and when should I use it。此外,您不需要 fiddle 和 File.separator
来组成 URL。
我目前正在尝试构建一个小网络工具,并尝试使用 DataExporter (Primefaces) 中的预处理器向 PDF 添加徽标。我已经设置了一个存储徽标的服务器。
现在,当我尝试将 DataTable 导出到 PDF 时,我不断收到此错误消息:
com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document
这就是包含预处理器方法的 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>
com.lowagie.text.Document cannot be cast to com.itextpdf.text.Document
此错误消息基本上意味着您正在尝试将类型 com.lowagie.text.Document
的对象转换为类型 com.itextpdf.text.Document
的对象,而该对象根本不是该对象的实例。这很可能发生在以下行中:
Document pdf = (Document) document;
您需要确保 Document
的 import
声明如下所示:
import com.lowagie.text.Document;
因此不是另一个。如果无法导入,请验证您使用的 iText 版本是否正确。确保您也没有多个不同版本的 iText 库。
与具体问题无关,永远不要使用getRealPath()
。而是使用 getResource()
或 getResourceAsStream()
。另见 What does servletcontext.getRealPath("/") mean and when should I use it。此外,您不需要 fiddle 和 File.separator
来组成 URL。