使用iText将外部图像添加到PDF [英] Adding external images to PDF using iText

查看:719
本文介绍了使用iText将外部图像添加到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使用iText将外部图像(由URL引用)添加到PDF。这种事情有可能吗?

I'm having trouble figuring out how to add an external image (referenced by a URL) to a PDF using iText. Is this kind of thing possible?

7.1.5中的PDF规范说你应该能够通过URL使用URL规范来引用PDF。这就是我到目前为止所做的:

The PDF spec in 7.1.5 says you should be able to reference a PDF via a URL by using a URL specification. This is what I've got so far:

PdfFileSpecification pdfSpec = 
    PdfFileSpecification.url(writer, "http://www.someurl.com/test.jpg");

StringBufferInputStream sbis = new StringBufferInputStream("");   
PdfStream dict = new PdfStream(sbis, writer);
dict.put(PdfName.FILTER, PdfName.DCTDECODE)
dict.put(PdfName.TYPE, PdfName.XOBJECT);
dict.put(PdfName.SUBTYPE, PdfName.IMAGE);
dict.put(PdfName.WIDTH, new PdfNumber(100));
dict.put(PdfName.HEIGHT, new PdfNumber(100));
dict.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
dict.put(PdfName.LENGTH, new PdfNumber(0));
dict.put(PdfName.F, pdfSpec);

PdfIndirectObject img = writer.addToBody(dict);

我知道我仍然需要确保添加色彩空间和东西,但我主要担心的是现在实际上是将这个图像放入文档的正文中。我无法弄清楚如何做到这一点......似乎我无法获得对PdfPage或资源字典或任何东西的引用。这是否可以使用iText?

I know I still need to make sure the color space is added and stuff, but my main concern right now is actually getting this image into the body of the document. I can't figure out how to do this... it seems I can't get a reference to a PdfPage or the resources dictionary or anything. Is this possible using iText?

作为附注,如果我在视图试图加载时会出现安全警告,则此练习无效图片。有谁知道是否是这种情况?

As a side note, this exercise is useless if I'm going to be presented with a security warning when the view tries to go load the image. Does anyone know if that is the case?

推荐答案

PDF规范中描述了外部内容,但实际上几乎没有PDF处理器支持他们。到目前为止,Acrobat 9已经支持它,但我对此功能非常谨慎:您的客户或用户可能无法看到引用的内容。

External content is described in the PDF spec, but almost no PDF processor does actually support them. By now Acrobat 9 has support for it, but I would be very cautious with that feature: Your clients or users may not be able to see the referenced content.

这篇关于使用iText将外部图像添加到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆