Gmane
Favicon Gravatar
From: Bruno Lowagie <bruno <at> lowagie.com>
Subject: Re: image form field
Newsgroups: gmane.comp.java.lib.itext.general
Date: 2006-02-10 16:36:12 GMT (3 years, 20 weeks, 4 days, 13 hours and 38 minutes ago)
Doug James wrote:

>I have not done any research, yet, so please be gentle if the answer is
>easy.
> 
>With an Acrobat form, can I set an image via PdfStamper and AcroFields?
>Like setting data fields.
> 
>
I just can't seem to get offline ;-)
This is actually a very good question!
The answer is no: you don't do it like setting data fields.
But yes: it's relatively easy to use a field as place-holder for an image:

PdfReader reader = new PdfReader("my.pdf");
PdfStamper stamper = new PdfStamper(reader, new 
FileOutputStream("new.pdf"));
AcroFields form = stamper.getAcroFields();
float[] photograph = form.getFieldPositions("photograph");
Rectangle rect = new Rectangle(photograph[1], photograph[2], 
photograph[3], photograph[4]);
Image img = Image.getInstance("my.jpg");
img.scaleToFit(rect.width(), rect.height());
img.setAbsolutePosition(photograph[1] + (rect.width() - 
img.scaledWidth()) / 2,
  photograph[2] + (rect.height() - img.scaledHeight()) / 2);
PdfContentByte cb = stamper.getOverContent((int)photograph[0]);
cb.addImage(img);
stamper.close();

br,
Bruno

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642