Paulo,
I've tried the code below, but I can't seem to understand it enough to
see why it's not working for me.
in the code below, the lines:
PdfDictionary dic = (PdfDictionary)item.widgets.get(0);
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.AP));
etc,
This seems to be setting the value of the "dic" variable over and over
again, right? Some of these calls are null in my source PDF. I've tried
going through and commenting then and uncommenting the dic=... line by
line but nothing seems to ever satisfy the "if obj.isIndirect".
My (very naive) guess is that perhaps it's because the logobutton
doesn't have an image already assigned to it. If my guess is correct,
then I'm not sure how to know when to insert the new image.
Also, when I use these lines:
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
with my image, the maskImage is returned as null, and the second line
for the addDirectImageSimple(img,
PRIndirectReference)obj) end up doing nothing as far as I can tell.
Any thoughts on what I'm doing wrong?
Thanks,
dave
Paulo Soares wrote:
Here's some code to do it. It has no error checking.
PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
AcroFields af = stp.getAcroFields();
AcroFields.Item item = af.getFieldItem("Button1");
PdfDictionary dic = (PdfDictionary)item.widgets.get(0);
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.AP));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.N));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.RESOURCES));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.XOBJECT));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.FRM));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.RESOURCES));
dic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.XOBJECT));
PdfObject obj = dic.get((PdfName)dic.getKeys().iterator().next());
if (obj.isIndirect()) {
PdfDictionary tg = (PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type =
(PdfName)PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type)) {
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img, (PRIndirectReference)obj);
}
}
stp.close();
Paulo
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of
> Dave White
> Sent: Sunday, June 04, 2006 3:58 PM
> To: [email protected]
> Subject: [iText-questions] Change Image button field in pre-existing PDF
>
> I see lots of great examples of how to create a new PushButton field
> and assign an image to it. I am stuck trying to figure out how to read
> a PDF that has a field already in it and assign a "logo" to the
> existing field. I am using AcroFields.setField to assign text values
> to other fields, but I can't figure out how to assign the button image
> as a result of calling getField, getFieldItem, etc.
>
> Is there a way to modify the button image of a field in an existing
> PDF template? In a worst case, it looks like I could insert a new
> PushButton in the exact same location as the existing one (and then
> try to add an image to it), but that doesn't feel "right" to do.
>
> This seems like it is either simple and I missing something easy, or
> impossible...
>
> Any help is MUCH appreciated.
>
> Thanks,
> dave
>
>
>
>
> _______________________________________________
> iText-questions mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
>
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta
transmissão desta mensagem não significa a perca de confidencialidade.
Se esta mensagem for recebida por engano, por favor envie-a de volta
para o remetente e apague-a do seu sistema de imediato. É proibido a
qualquer pessoa que não o destinatário de usar, revelar ou distribuir
qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may
contain confidential or legally protected information. The incorrect
transmission of this message does not mean the loss of its
confidentiality. If this message is received by mistake, please send it
back to the sender and delete it from your system immediately. It is
forbidden to any person who is not the intended receiver to use,
distribute or copy any part of this message.
------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions