向pdf添加损坏或错误类型的图像时发生java错误
我在将图像插入到使用Itext7创建的PDF文档时遇到问题。图像在my DB中存储为byte[],它可能有损坏的字节或格式错误。 这是我的清单:
Image imageLeft;
byte[] logo = a.getLogo();
if (logo == null) {
String logoPath = this.getClass().getResource("/com/lgs/bl/resource/bl-nologo.png").getPath();
imageLeft = new Image(ImageDataFactory.create(logoPath, true));
imageLeft.setFixedPosition(60, 730);
imageLeft.scaleToFit(250, 140);
try (Document document = new Document(pdfDoc)) {
document.add(imageLeft);
...
}
有时我会出现以下错误:
com.itextpdf.io.IOException: Image format cannot be recognized.
at com.itextpdf.io.image.ImageDataFactory.createImageInstance(ImageDataFactory.java:484)
at com.itextpdf.io.image.ImageDataFactory.create(ImageDataFactory.java:76)
at com.lgs.bl.web.invoices.pdf.InvoicePdfCreate.fillFieldsDoppio(InvoicePdfCreate.java:376)
我怎样才能确保避免这个问题,我能抓到一些丢弃的东西吗?谢谢
# 1 楼答案
正如Bruno Lowagie所建议的,捕获异常并解决问题是非常简单和容易的。非常感谢