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

Reply via email to