- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 IText XMLWorker 中处理嵌入的图像。
有没有办法在 XMLWorker 中处理嵌入式 (Base64) 图像?在版本 5.3.5我使用的 ImageProvider 不再工作(之前引发异常),所以我按如下方式修补了 ImageRetrieve,但显然这将在接下来被打破XMLWorker 更新:
package com.itextpdf.tool.xml.net;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.codec.Base64;
import com.itextpdf.tool.xml.net.exc.NoImageException;
import com.itextpdf.tool.xml.pipeline.html.ImageProvider;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author redlab_b
*
*/
public class ImageRetrieve {
final static Pattern INLINE_PATTERN = Pattern.compile("^/data:image/(png|jpg|gif);base64,(.*)");
private final ImageProvider provider;
/**
* @param imageProvider the provider to use.
*
*/
public ImageRetrieve(final ImageProvider imageProvider) {
this.provider = imageProvider;
}
/**
*
*/
public ImageRetrieve() {
this.provider = null;
}
/**
* @param src an URI that can be used to retrieve an image
* @return an iText Image object
* @throws NoImageException if there is no image
* @throws IOException if an IOException occurred
*/
public com.itextpdf.text.Image retrieveImage(final String src) throws NoImageException, IOException {
com.itextpdf.text.Image img = null;
if (null != provider) {
img = provider.retrieve(src);
}
if (null == img) {
String path = null;
if (src.startsWith("http")) {
// full url available
path = src;
} else if (null != provider){
String root = this.provider.getImageRootPath();
if (null != root) {
if (root.endsWith("/") && src.startsWith("/")) {
root = root.substring(0, root.length() - 1);
}
path = root + src;
}
} else {
path = src;
}
if (null != path) {
try {
Matcher m;
if (path.startsWith("http")) {
img = com.itextpdf.text.Image.getInstance(path);
} else if ((m = INLINE_PATTERN.matcher(path)).matches()) {
// Let's handle the embedded image without saving it
try {
byte[] data = Base64.decode(m.group(2));
return Image.getInstance(data);
} catch (Exception ex) {
throw new NoImageException(src, ex);
}
} else {
img = com.itextpdf.text.Image.getInstance(new File(path).toURI().toURL());
}
if (null != provider && null != img) {
provider.store( src, img);
}
} catch (BadElementException e) {
throw new NoImageException(src, e);
} catch (MalformedURLException e) {
throw new NoImageException(src, e);
}
} else {
throw new NoImageException(src);
}
}
return img;
}
}
最佳答案
你问这个问题已经快一年了,但也许这个答案无论如何都会有所帮助。
最近遇到了类似的问题。我的目标是在生成的 pdf 中包含存储在数据库中的图像。
为此,我扩展了 com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider
类并覆盖了它的 retrieve()
方法,如下所示:
public class MyImageProvider extends AbstractImageProvider {
@Override
public Image retrieve(final String src) {
Image img = super.retrieve(src);
if (img == null) {
try {
byte [] data = getMyImageSomehow(src);
img = Image.getInstance(data);
super.store(src, img);
}
catch (Exception e) {
//handle exceptions
}
}
return img;
}
@Override
public String getImageRootPath() {
return "http://sampleurl/img";
}
}
然后,在为 XMLWorker [1] 构建管道时,我将我的类的一个实例传递给上下文:
htmlPipelineContext.setImageProvider(new MyImageProvider());
现在我们希望这应该有效。但是有一个陷阱!在 xmlworker 库的深处某处,正在克隆此 htmlPipelineContext。在此操作期间,我们的 ImageProvider 实现丢失了。这是在 HtmlPipelineContext 的 clone() 方法中发生的。看一下第274-280行(我指的是5.4.4版本):
final String rootPath = imageProvider.getImageRootPath();
newCtx.setImageProvider(new AbstractImageProvider() {
public String getImageRootPath() {
return rootPath;
}
});
这甚至在 HtmlPipelineContext.clone() 的 javadoc [2] 中有描述:
Create a clone of this HtmlPipelineContext, the clone only contains the initial values, not the internal values. Beware, the state of the current Context is not copied to the clone. Only the configurational important stuff like the (...) ImageProvider (new AbstractImageProvider with same ImageRootPath) , (...) are copied.
这不是很好笑吗?您通过将其抽象化而获得设计用于扩展的类,但最终证明该类仅用作属性持有者。
我的解决方法:
public class MySpecialImageProviderAwareHtmlPipelineContext extends HtmlPipelineContext {
MySpecialImageProviderAwareHtmlPipelineContext () {
super(null);
}
public HtmlPipelineContext clone () {
HtmlPipelineContext ctx = null;
try {
ctx = super.clone();
ctx.setImageProvider(new MyImageProvider());
} catch (Exception e) {
//handle exception
}
return ctx;
}
}
然后我就用这个代替 HtmlPipelineContext。
[1] http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-7
[2] http://api.itextpdf.com/xml/com/itextpdf/tool/xml/pipeline/html/HtmlPipelineContext.html#clone()
关于image - 在 IText XMLWorker 中处理嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864369/
iText 文档指出它只包含特定的字体子集,但从未说明它们是什么。有没有人知道 iText 中默认包含哪些字体? (我在网上搜索过,在任何地方都找不到这个字体列表!) 最佳答案 它可能指的是PDF S
我使用固定列宽创建了下表,如下所示, Table headerTable = new Table(new float[]{5,5,5}); headerTable.setWidthPercent(
我正在尝试使用 iText 缩放图像(在新的 PDF 文档上)以使其填充页面宽度而不拉伸(stretch),这样它可能需要几页。 我找到了很多解决方案,但它们都非常复杂,而且我真的不喜欢那样编码。到目
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
如何使用 iText 找到文档中的行的位置? 假设我有一个 PDF 文档中的表格,并且想要阅读其中的内容;我想找到细胞的确切位置。为了做到这一点,我想我可能会找到线条的交点。 最佳答案 我认为您使用
请找到下面的代码。 public class MakingFieldReadOnly implements PdfPCellEvent { /** The resulting PDF. */
在 iText 5 中有一个名为 getVerticalPosition() 的方法,它给出了下一个写入对象在页面上的位置。作为回答这个问题 How to find out the current c
抱歉,如果有类似我的帖子,但我是这个论坛的新手,我还没有找到它。 我有动态调整 TextField 大小取决于文本大小的问题。我填写现有的 PDF - 在 AcroForm 中填写字段: form.s
我正在使用 itext 生成 pdf。因此,当页面内容超出时,它会自动创建一个新页面。我想知道它是否创建了一个新页面。如果是,我想在页面顶部添加一些图像。 List paylist =new List
我的 有问题固定 表格左侧和右侧的边距。 我想删除该边距并使用没有边距或填充的所有工作表。我该怎么办? 我刚刚试过这个,但对我不起作用: cell.setPaddingLeft(0); cell.se
我有 2 行,我想对齐(证明)它们。 我有这个代码: Paragraph p=new Paragraph(ANC,fontFootData); p.setLeading(1, 1);
我有这样的场景。 我有一个生成 PDF 的应用程序,需要对其进行签名。 我们没有用于签署文档的证书,因为它们位于 HSM 中,而我们使用证书的唯一方法是使用 Web 服务。 此网络服务提供两个选项,发
我需要实现一个看起来像图片中的表格,列之间有空间。我试过: cell.setPaddingLeft(10); cell.setMarginLeft(10); extractio
我需要实现一个看起来像图片中的表格,列之间有空间。我试过: cell.setPaddingLeft(10); cell.setMarginLeft(10); extractio
我需要使用 Java 的 iText 库创建一个 PDF 文档。我还需要包括一些复选框,这些复选框根据某些类变量的值打开/关闭。我找到了一些关于交互式表单的示例,但我不需要这种复杂程度:只需将一些复选
我正在开发一个系统,我必须在其中将一些图像添加到现有的 PDF 文档中。 这适用于 iText 5.1.3,但由于某种原因,在包含扫描图像的 PDF 中,它不会添加任何图像。 这是 PDF Docum
我们正在研究信息提取,我们想使用iText。 我们正在探索 iText。根据我们查阅过的文献,iText 是最好的工具。是否可以从 iText 中每行的 pdf 中提取文本?我在与我的相关的 stac
我已经创建了一个带有一些文本字段的 pdf 文档。我可以使用 Adobe 阅读器填充这些文本字段并将这些值保存在该文件中。 我的问题是,我可以使用 iText 以编程方式执行此操作吗?如果可能,请
我正在使用 iText 5 表创建标签(如 Avery 标签)。标签元素的定位需要一些非常严格的公差,以便适合标签上的所有内容。我的问题是标签上有多个区域为 PdfPCells。我需要将文本放入这些区
我是一名优秀的程序员,十分优秀!