1

I want to add image to a specific location in a PDF file using iText in Android. This is a fillable form and I have added textbox that is a place holder for the image and what I want to do is to get that textbox and image to it like this.

public class FormFill {
    public static void fill(AcroFields form, Person person) throws IOException, DocumentException{
        form.setField("firstname", person.getFirstName());
        form.setField("lastname", person.getLastName());
        form.setField("imagetextbox", "???");   

    }

I have the image uri like so

Uri imageUri = Uri.parse(person.getImagePath());

Any help would be appreciated.

Val Okafor
  • 3,371
  • 13
  • 47
  • 72
  • There's a good example here: http://stackoverflow.com/questions/8656844/how-can-i-set-an-image-to-a-pdf-field-in-existing-pdf-file – Bruno Lowagie Apr 28 '15 at 15:37
  • Bruno thank you so much, however for some reason I am not able to get that example to work, I ended up using PdfContentByte – Val Okafor Apr 29 '15 at 18:40

2 Answers2

3

try this. i have used this function for add image in document.

public void addLogo(Document document) throws DocumentException {
    try { // Get user Settings GeneralSettings getUserSettings =

        Rectangle rectDoc = document.getPageSize();
        float width = rectDoc.getWidth();
        float height = rectDoc.getHeight();
        float imageStartX = width - document.rightMargin() - 315f;
        float imageStartY = height - document.topMargin() - 80f;

        System.gc();

        InputStream ims = getAssets().open("splashscreen.jpg");
        Bitmap bmp = BitmapFactory.decodeStream(ims);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        byte[] byteArray = stream.toByteArray();
        // PdfImage img = new PdfImage(arg0, arg1, arg2)

        // Converting byte array into image Image img =
        Image img = Image.getInstance(byteArray); // img.scalePercent(50);
        img.setAlignment(Image.TEXTWRAP);
        img.scaleAbsolute(200f, 50f);
        img.setAbsolutePosition(imageStartX, imageStartY); // Adding Image
        document.add(img);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
  • Wow, this solution looks great and I wish I can get it to work, I do not have an instance of a Document though, I have an instance of a Stamper. I will try to see if I can make it work. – Val Okafor Apr 29 '15 at 18:42
1

I ended adding the image like so

                Image image = Image.getInstance(stream.toByteArray());
                PdfContentByte overContent = stamper.getOverContent(1);
                overContent.addImage(image);
Val Okafor
  • 3,371
  • 13
  • 47
  • 72