Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding external images to PDF using iText

Tags:

image

pdf

itext

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?

like image 753
MikeQ Avatar asked Dec 02 '09 14:12

MikeQ


1 Answers

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.

like image 89
khkremer Avatar answered Sep 23 '22 01:09

khkremer