Add image to acrofield in iText?
Asked Answered
R

3

11

I'm trying to fill PDF using acrofields, I'm able to add string data perfectly, but having issues in adding images to acrofields. This is my code for adding string data..

    File f = new File("F:/Test/Agreement.pdf");
    InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f));
    File destinationFile = new File("F:/Test/ag1.pdf");

    PdfReader reader = new PdfReader(sourceTemplatePDFUrlStream);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
            destinationFile));

    AcroFields form = stamper.getAcroFields();
    Image img = Image.getInstance("E:/signature/signature.png");
    Set fields = form.getFields().keySet();

    Hashtable val = new Hashtable();
    val.put("name", "xxx" );
    val.put("city_street_zip", "xxx"+"                    "+"xxx"+"                "+"xxx");
    val.put("chck_1", "Yes" );
    val.put("chck_2", "No");
    val.put("chck_3", "Yes" );
    val.put("street_address", "xxx" );
    val.put("account_num", "1234");



    Enumeration enumeration = val.keys();

    // iterate through Hashtable val keys Enumeration
    while (enumeration.hasMoreElements()) {
        String nextElement = (String) enumeration.nextElement();
        String nextElementValue = (String) val.get(nextElement);
        //System.out.println(nextElement + ":=================fillData===================:" + nextElementValue);
        form.setField(nextElement, nextElementValue);
    }

    //Form flattening makes the form non-editable and saveable with the 
    //form val filled in
    stamper.setFormFlattening(true);

    stamper.close();
Rest answered 17/4, 2013 at 5:15 Comment(2)
@VigneshVino edited my question..included codeRest
your stack trace will help me to understand u'r issueCharlyncharm
A
15

The "official" way to do this, is to have a Button field as placeholder for the image, and to replace the "icon" of the button as described in my book:

PushbuttonField ad = form.getNewPushbuttonFromField(imageFieldName);
ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
ad.setProportionalIcon(true);
ad.setImage(Image.getInstance("E:/signature/signature.png"));
form.replacePushbuttonField("advertisement", ad.getField());

See ReplaceIcon.java for the full code sample.

DISCLAIMER: I'm the original developer of iText and the author of the "iText in Action" books.

Ankylose answered 17/4, 2013 at 9:48 Comment(3)
Thanks for the reply!! It's very nice to get reply from you :)Rest
I like the concept of StackOverflow. That's why I joined last year. I try answering a couple of questions a day (although I don't always have the time).Ankylose
form.getNewPushbuttonFromField(imageFieldName) return null for meShotwell
R
5

The following solution worked:

public static void addImage(PdfStamper stamper,AcroFields form,String field,String fieldValue){
    try{
        System.out.println("Field "+field);
    java.util.List<AcroFields.FieldPosition> photograph = form.getFieldPositions(field);
    if(photograph!=null && photograph.size()>0){
    Rectangle rect= photograph.get(0).position;
    //if(StringUtils.isNotBlank(fieldValue)){
    Image img = Image.getInstance(fieldValue);
    img.scaleToFit(rect.getWidth(), rect.getHeight());
    img.setBorder(2);
    img.setAbsolutePosition(
    photograph.get(0).position.getLeft() + (rect.getWidth() - img.getScaledWidth() )
    , photograph.get(0).position.getTop() - (rect.getHeight()));
    PdfContentByte cb = stamper.getOverContent((int)photograph.get(0).page);
    cb.addImage(img);
    //}
    }
    }catch(Exception e){
        e.printStackTrace();
    }
    }

calling the above method:

addImage(stamper, form, "CustomerSign", "E:/signature/signature.png");

where CustomerSign is AcroField

Rest answered 17/4, 2013 at 7:43 Comment(0)
C
0

You can try add this with your code for adding Image

PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages());
Image image = Image.getInstance(new URL("E:/signature/signature.png"));
image.setAbsolutePosition(450,650);
image.scaleAbsolute(200,200);
content.addImage(image);
reader.close();
return output.toByteArray();
Charlyncharm answered 17/4, 2013 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.