I had a PDF document which has acro text fields which is already shared to the client. Now the client wants to insert signature image in one of the text fields. My manager asked me to try a way to do the same.My idea is to replace an image on top of the text field position and resize the text field as image size.
For replacing acro text field of pdf into image, i am trying as below
1.Finding the text field by its field id
String position = null;
List<FieldPosition> fieldPositons = form.getFieldPositions("50106");
for (FieldPosition position :fieldPositons) {
this.position = position.position;
}
2. Setting the image into that position of text field
Image image = Image.getInstance("ImageFileName");
Float dimensions = position.split("x");
image.setAbsolutePosition(dimensions[0], dimensions[1]);
content.addImage(image);
Based on the given image width and height i need to change the width and height of acro text field.
Can any one tried as below, Is my logic works with itext pdf library. Let me know if any idea to replace acro text field with image
It is never a good idea to change the dimensions of an AcroField as all the content in a PDF is added at absolute positions. When you change the dimension of a field, you risk that it will overlap with other content.
Hence, your best option is to adapt the size of the image to the size of the AcroField. As you already indicated, you can get the field position like this:
Note that it's not a good idea to make this a
string
. You can use theFieldPosition
object like this:You can scale and position the image like this:
The
ScaleToFit()
method will scale the image in such a way that it will fit the dimensions of the form field, respecting the original aspect ratio of the image (you don't want to add a stretched image).You need to
page
variable to add the image to the correct page:Important: