Closed shavo007 closed 6 years ago
I added another test method to the CreateImageButton
test class: testUpdateSimpleImageButton
. It is based on your code with one relevant change: I removed the line
acrobatAcroForm.getFields().add(button);
The reason: The button
form field already is in that list, after all you retrieved it using acrobatAcroForm.getField("PushButton")
. Adding it again is not appropriate as it generates a duplicate entry.
And another caveat: The code as is only works well if the image has approximately the same aspect ratio as the button. If that cannot be guaranteed, you should replace the argument in
pdAppearanceStream.setBBox(new PDRectangle(width, height));
by a larger rectangle, by the smallest larger rectangle that has the same aspect ratio as the button.
Awesome work @mkl-public . Thanks for your prompt response.
The aspect ratio is definitely something to consider.
I forgot to ask is this the best approach to add images to a template?
"The best approach"? Well, it depends.
First of all, using some annotation (e.g. a button like you do) to mark the area for an image is a good idea.
There are two major approaches for filling in, though: Either, as you do, one sets the button appearance to a XObject filled with the image; or, alternatively, one retrieves the coordinates of the field, then drops the field and adds the image at the given coordinates to the page content stream.
The advantage of the latter approach is that there still are some very incomplete PDF viewers which have issues with annotations in general; such viewers would probably not display your image correctly. (There used to be many such viewers but their relevance has been dropping more and more, so nowadays this advantage is merely slim.)
Furthermore, there are two differences between the approaches which are not generally an advantage of one or the other approach but depending on the use case may favor one or the other:
Using the former approach the image can be re-set again and again using the same code. The button whose appearance one must set is still there, after all. Using the latter approach that is not possible using the same code, and using different code is more difficult as the named button is gone.
Also, if the file is digitally signed after the image has been set using the former approach, changing it yet again does not necessarily (depending on the certification level) invalidate the signature; if the file is digitally signed after applying the latter approach, manipulating the image does invalidate the signature.
@mkl-public thanks for detailed explanation
Hi,
I seen your test case for creating a button and adding an image appearance.
Im looking to use an existing button (as a placeholder) on the pdf to draw an image. Is this the correct approach?
If so does this code look correct? (This is based of the test you defined)
Thanks, Shane.