tags:

views:

606

answers:

1

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?

+4  A: 

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.

khkremer
Hmm, thanks very much, that is helpful. What I'm really trying to do is just get the document to contact a server in the background (was just going to embed a 1px image). I'm working with a client who wants to generate some sensitive documents for their customers and detect if they're being copied all around.
MikeQ
...and of course I quickly found out that the Adobe reader has had protections against this sort of thing since version 7 (it shows a security warning, which is a good thing).
MikeQ