0

I have a template pdf with 2 PushbuttonField, what I want is , If an Image is very small, to not resize it, I have try to use setProportionalIcon(false);but It doesn't work, here my code:

PdfReader reader = new PdfReader("C:/Users/Desktop/template.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/Users/Desktop/result.pdf"));
AcroFields form = stamper.getAcroFields();
PushbuttonField ad = form.getNewPushbuttonFromField("first");
ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
ad.setProportionalIcon(true);//or false but doesn't work
ad.setImage(Image.getInstance("C:/Users/Desktop/first.jpg"));
form.replacePushbuttonField("Fronte", ad.getField());


PushbuttonField ad1 = form.getNewPushbuttonFromField("second");
ad1.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
ad1.setProportionalIcon(true);//or false but doesn't work
ad1.setImage(Image.getInstance("C:/Users/Desktop/second.jpg"));
form.replacePushbuttonField("Retro", ad1.getField());


stamper.setFormFlattening(true);
stamper.close();
reader.close();
Liz Lamperouge
  • 681
  • 14
  • 38
  • If you're flattening anyway, why don't you just retrieve the coordinates of the field, and stamp the image on the document using those coordinates instead of using the `replacePushbuttonField()` method. That way, you have full control over the size of the image. – Bruno Lowagie Jun 19 '17 at 14:03
  • @Bruno Lowagie for example ? I don't have understand your answer – Liz Lamperouge Jun 19 '17 at 14:09
  • 1
    I don't understand what you don't understand. You have a push button in the existing PDF. That means that you have coordinates. If you have coordinates, you can add an image at those coordinates, can't you? You don't need `replacePushbuttonField()` to do that, do you? – Bruno Lowagie Jun 19 '17 at 15:22
  • I have never used images in such way, How I can put the image in the coordinate ? How I can get the coordinate of this PushButton ? – Liz Lamperouge Jun 19 '17 at 17:01
  • That's in the documentation: [How to find the absolute position and dimension of a field?](http://developers.itextpdf.com/question/how-find-absolute-position-and-dimension-field) and [How to stamp image on existing PDF and create an anchor?](http://developers.itextpdf.com/question/how-stamp-image-existing-pdf-and-create-anchor) Or better yet: [How to show an image at a text field position?](http://developers.itextpdf.com/question/how-show-image-text-field-position) It doesn't matter that your field is a push button instead of a text field. Both are form fields so the answer is still valid. – Bruno Lowagie Jun 20 '17 at 06:25
  • I will try it and let you know, thank you – Liz Lamperouge Jun 20 '17 at 07:11
  • I have add this, but It do nothing... image.scaleAbsolute(50, 20); image.setAbsolutePosition(41, 347); image.setAnnotation(new Annotation(0, 0, 0, 0, 3)); PdfContentByte content = stamper.getOverContent(1); content.addImage(image); – Liz Lamperouge Jun 20 '17 at 09:52
  • You don't need the annotation if the image doesn't need to be "clickable." How did you choose the coordinates `41, 347`? Are you sure those coordinates are in the visible area of the page? Did you remove the button annotation or is it covering the image? – Bruno Lowagie Jun 20 '17 at 10:03
  • Why I have to remove the button annotation ? I want that the image is exactely on the field of the PushbuttonField area, the 2 value are the values of llx and lly of the rectangle of the PushbuttonField: Rectangle rectangle = form.getFieldPositions("first").get(0).position; – Liz Lamperouge Jun 20 '17 at 10:11
  • There is no button annotation. There is a widget annotation of a button field. That widget annotation is merely a placeholder. You want to add the image exactly at the coordinates of that button field, but you don't need the button field for anything else than the coordinates (otherwise you wouldn't flatten the form). – Bruno Lowagie Jun 20 '17 at 10:19
  • I don't understand your answers, why do you previously ask me "Did you remove the button annotation or is it covering the image?" If now you are telling me that "there is no button annotation"? "You want to add the image exactly at the coordinates of that button field, but you don't need the button field for anything else than the coordinates (otherwise you wouldn't flatten the form)" And so what? – Liz Lamperouge Jun 20 '17 at 10:25
  • OK, *button annotation* was short for the *button field's widget annotation*. So... did you remove it once you obtained its coordinates? You are aware of the fact that thousands of developers had no problem achieving the simple task that is taking you so long, aren't you? One reason why it is so hard to help you, is that you aren't very helpful either. You don't provide a [SSCCE](http://sscce.org); all you are saying is that "it doesn't work." I'm pretty sure that's why no one else is answering. – Bruno Lowagie Jun 20 '17 at 10:30
  • Ok, has I have wrote, and has I have read on the utils, the method setProportionalIcon(false) "Sets the way the icon is scaled. If true the icon is scaled proportionally, if false the scaling is done anamorphically" So, I used "false" but nothing change, why? – Liz Lamperouge Jun 20 '17 at 10:59
  • To be more specific, I want to put the image in this PushbuttonField , If the image is too small, to not resize it, but If not (though is too big) to resize it. – Liz Lamperouge Jun 20 '17 at 11:02
  • Maybe you don't understand the meaning of "the scaling is done anamorphically". The scaling is always done, but when you use `true` it is done proportionally; when you use `false`, it is done anamorphically. – Bruno Lowagie Jun 20 '17 at 11:04
  • Ok, now I have really understand the meaning of "the scaling is done anamorphically", I thought that with the false status in setProportionalIcon, It didn't scaling the image... I have now tried to not use at all setProportionalIcon, but the result is the same as when I used it; is really helpfull this method? – Liz Lamperouge Jun 20 '17 at 13:22
  • I give up. If you insist on using `replacePushbuttonField()` instead of using the solution as proposed in the answer to the question [How to show an image at a text field position?](http://developers.itextpdf.com/question/how-show-image-text-field-position) you are on your own. There is only so much time I can give to a person for free. – Bruno Lowagie Jun 20 '17 at 13:26
  • @Bruno Lowagie nobody can answer this questions because you have disabled the answer. Thank you for the time that you gave me . – Liz Lamperouge Jun 20 '17 at 13:28
  • I've reopened it. Let's see if someone else will answer. – Bruno Lowagie Jun 20 '17 at 14:25

0 Answers0