Adding external images to PDF using iText

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

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?

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); 

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!