There is a way to put an image in a AcroForm field.
Here's a method I wrote and use. It takes a PDF input file, a PDF output file, an image file, and a location to put the image.
It is basically one of the FAQ entries.. The image grows to fill the size of the field.

    public static void createPDFWithImageInAcroField(String pInFile, String pOutFile, String pImageFile, String pPDFField) throws Exception       
    {
        if (pInFile==null || pOutFile==null || pImageFile==null || pPDFField==null) {              
            throw new Exception("Method createPDFWithImageInAcroField() requires non-NULL parameters"+
                            " for InputPDF, OutputPDF, ImageFile, and AcroFormFieldName");
        }
        PdfReader reader = new PdfReader(pInFile);
        PdfStamper stamper = new PdfStamper(reader,
                                            new FileOutputStream(pOutFile));
        AcroFields form = stamper.getAcroFields();
        float[] photograph = form.getFieldPositions(pPDFField);
        Rectangle rect = new Rectangle(
                                      photograph[1], photograph[2], photograph[3], photograph[4]);
        Image img = Image.getInstance(pImageFile);
        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(1);
        cb.addImage (img);
        stamper.close();

    }





On 9/8/06, Mark Storer <[EMAIL PROTECTED] > wrote:
>Is there a way to use iText to set an imagefield in an acro form similar to what can be done with textfields?

Kinda.  There's no such thing as an "image field" in an AcroForm.  That being said, you can set a button's appearance to "Icon Only", and draw whatever you like for the 'icon'... in theory.

Creating a button field from scratch with an icon appearance is fairly simple with PushbuttonField.  Modifying an existing button is considerably more challenging, and requires significant PDF knowledge.  PdfContentByte's drawButton() won't help.  Nor will AcroFields.getAppearance().

The only realistic option for you would be something like this:

------------

AcroField.Item fldItem = form.getFieldItem( "buttonName" )

for (int i = 0; i < fldItem.widgets.size(); ++i) {
    Pushbutton button = new PushButton(...);
    PdfDictionary curWidg = (PdfDictionary)fldItem.widgets.get( i );
    PdfDictionary curMerged = (PdfDictionary)fldItem.merged.get( i );

    myAttributeSetter( button, curMerged ); // exercise for the reader

    PdfFormField tmpField = button.getField();

    PdfObject apObj = tmpField.get( PdfName.AP );
    PdfObject mkObj = tmpField.get ( PdfName.MK ):

    curWidg.put( PdfName.AP, apObj );
    curMerged.put (PdfName.AP, apObj );

    curWidg.put( PdfName.MK, mkObj );
    curMerged.put( PdfName.MK, mkObj );
}

------------

myAttributeSetter() will be "non-trivial", particuarly for someone unfamiliar with PDF.  Bounding boxes, field flags, etc, etc.  You'll want to take a long look at chapter 8's section on form fields in the PDF Reference.

--Mark Storer
  Senior Software Engineer
  Cardiff Software
#include <disclaimer>
typedef std::Disclaimer<Cardiff> DisCard;

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions




--
------------------------------------------------------------------
Hand Crafted Magic
http://www.theambitiouscard.com/
------------------------------------------------------------------
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to