渲染图像XmlWorkerHelper(itextpdf)

编程入门 行业动态 更新时间:2024-10-28 13:25:16
本文介绍了渲染图像XmlWorkerHelper(itextpdf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请帮助我.

我在用pdf渲染图像时遇到问题 即时通讯使用itextpdf 5.5.6和itext 5.5.6

I have problem with rendering image in pdf im usung itextpdf 5.5.6 and itext 5.5.6

我的代码是:

. . . URL url = new URL("some.html"); URLConnection uc = url.openConnection(); InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream()); XMLWorkerHelper worker = XMLWorkerHelper.getInstance(); worker.parseXHtml(pdrwriter, doc, inputStreamReader); doc.close(); //close the writer pdrwriter.close();

我的html有:

<table><tr><td><img src="mysite/logo.jpg" /></td</tr></table>

,我得到了错误: 找到无效的嵌套标签td,预期关闭标签img.

and i get error: Invalid nested tag td found, expected closing tag img.

我尝试过

<table><tr><td><img src="mysite/logo.jpg"></img</td</tr></table>

sam错误..

您知道如何处理吗?谢谢!!!

do You know how to handel it? Thx for help!!!

推荐答案

如果您要解析的HTML文件存储在与工作目录不同的目录中,则iText将无法创建Image对象.我们必须提供ImageProvider接口的实现,该实现告诉iText如果遇到img标签该怎么办.此界面具有以下方法.

If the HTML file you're parsing is stored in a directory that is different from the working directory, iText won't be able to create Image objects. We have to supply an implementation of the ImageProvider interface that tells iText what to do if an img tag is encountered. This interface has the following method.

Image retrieve(final String src); String getImageRootPath(); void store(String src, Image img); void reset();

您可以编写自己的类来实现这四个方法,或者可以对AbstractImageProvider进行子类化.最好是后者.

You can write your own class implementing these four methods, or you can subclass AbstractImageProvider. It is preferred to do the latter.

XML Worker将使用AbstractImageProvider类的store()方法来缓存Map中遇到的所有Image对象.当对具有相同src的图像调用retrieve()方法时,这些对象将被重用.如果您不缓存图像,则PDF将会will肿.相同的图像位和字节将多次写入PDF. reset()方法清除缓存;克隆ImageProvider时使用它.最后,未实现getImageRootPath()方法.您必须自己实现它,如以下代码片段

XML Worker will use the store() method of the AbstractImageProvider class to cache all the Image objects that are encountered in a Map. These objects will be reused when the retrieve() method is called for an image with the same src. If you don't cache images, your PDF will be bloated. The same image bits and bytes will be written to the PDF more than once. The reset() method clears the cache; it is used when an ImageProvider is cloned. Finally, the getImageRootPath() method isn't implemented. You have to implement it yourself, as is done in the following snippet

htmlContext.setImageProvider(new AbstractImageProvider() { public String getImageRootPath() { return "src/main/resources/html/"; } });

更多推荐

渲染图像XmlWorkerHelper(itextpdf)

本文发布于:2023-10-30 01:27:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   XmlWorkerHelper   itextpdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!