action item to create image field using javascript in adobe acrobat - PDF Forms
Copy link to clipboard
Copied
I am trying to create Action using Javascript to insert Image field at the bottom of page 1 of a document and it has to be duplicated towards end of the document.
Please help me with code.
I am all new to scripting
Copy link to clipboard
Copied
i tried using the below for Text field but i am in need of creating Image field
please help
for (var p=0; p<this.numPages; p++) { var f = this.addField("Today", "text", p, [222.6179962158203,775.823974609375,372.6180114746094,753.823974609375]); f.readonly = true; }
Copy link to clipboard
Copied
Image fields are actually buttons, so you need to add a button field, not a text field.
Then you need to set it as having an icon (using the buttonPosition property) and finally you can import an image to it using the buttonImportIcon method. Read the documentation of these properties and methods to see exactly how to do that.
Copy link to clipboard
Copied
thank you for the guidance.
i will work on it and getback
Copy link to clipboard
Copied
@try67 please help
Copy link to clipboard
Copied
could you help me with code for the same please. As i am all new to adobe coding
position i need button is 200.5939,122.6608,367.1745,76.9592
i tried doing the below but it is not working
for (var p=0; p<this.numPages; p++) {
var f = this.addField("Image43", "button", p, [200.5939,122.6608,367.1745,76.9592]);
f.setAction(this.buttonImportIcon());
f.strokeColor = color.red;
}
Copy link to clipboard
Copied
@try67 tried the below too, but not working. Request your help with code
function onclick()
{
event.target.buttonImportIcon();
}
for (var p=0; p<this.numPages; p++) {
var f = this.addField("Sign", "button", p, [200.5939,122.6608,367.1745,76.9592]);
f.strokeColor = color.red;
f.setAction(onclick);
}
Copy link to clipboard
Copied
Check the Javascript console for errors.
Copy link to clipboard
Copied
- setAction requires a trigger. It also requires a string as the code to execute.
I don't see why you need to use a function for this, but you can, if you do it correctly.
- You didn't set the buttonPosition property, as I've indicated.
Copy link to clipboard
Copied
for (var p=0; p<this.numPages; p++) {
var firstButton = this.addField("Button 1", "button", 1, [200.5939,122.6608,367.1745,76.9592]);
firstButton.strokeColor = color.red;
firstButton.buttonPosition=position.iconOnly;
firstButton.buttonImportIcon();
}
i tried the above code, before the button is clicked, window to browse is popping up
this should happen only on button click
request help with the code please
Copy link to clipboard
Copied
I thought that's what you wanted... If you want it to only happen when the button is clicked then change this line:
firstButton.buttonImportIcon();
To:
firstButton.setAction("MouseUp", "event.target.buttonImportIcon();");
Copy link to clipboard
Copied
thank you so much @try67 it worked
I have another query,
I have a form created using Prepare Form option and Image Field, another Form created using the Javascript option. Though both the Image fields are created using same name but if i upload picture in manually created form it is not getting reflected in form created using Javascript.
Could you please help
Copy link to clipboard
Copied
Are all image fields in the same document?
Copy link to clipboard
Copied
Yes Images are in same document
Copy link to clipboard
Copied
Each button can show its own image, even if they have the same name. They do not act like text fields where the value is always the same if they have the same name, as an image is not a value. In fact, one button can show an icon and the other just text.
Copy link to clipboard
Copied
If you want to import the selected image to all "widgets" of the button field in the group, change your code to this:
this.getField("Button1").buttonImportIcon();
(replace "Button1" with the actual field name, of course)
Copy link to clipboard
Copied
@try67 If i am using the below code, window to browse is popping up immediately when i run the Action, i have to close all the pop up windows to access the form and nothing is working when the button is clicked after that
for (var p=0; p<this.numPages; p++) {
var firstButton = this.addField("Image43", "button", p, [200.5939,122.6608,367.1745,76.9592]);
firstButton.strokeColor = color.red;
firstButton.buttonPosition=position.iconOnly;
this.getField("Image43").buttonImportIcon();
}
Copy link to clipboard
Copied
You need to use it as the string for the setAction method, like this:
firstButton.setAction("MouseUp", "this.getField(event.target.name).buttonImportIcon();");
Copy link to clipboard
Copied
Thank you so much @try67 for helping me out. This was much needed to reduce all the manual and monotonus work
the below code is used to create Image Field in all the pages on the document
for (var p=0; p<this.numPages; p++) {
var firstButton = this.addField("Image43", "button", p, [200.5939,122.6608,367.1745,76.9592]);
firstButton.strokeColor = color.red;
firstButton.buttonPosition=position.iconOnly;
firstButton.setAction("MouseUp", "this.getField(event.target.name).buttonImportIcon();");
}
However, Image Field created using Prepare Form option and Image Field created using the above code are not identical though Field name is same in both.
It should have been more helpful if both the fields that are created using manual and javascript are identical or duplicates
As heard, it is restriction from Adobe..
Copy link to clipboard
Copied
Try this:
Change the action of the manual created image field.
Copy link to clipboard
Copied
Thank you @Bernd Alheit
is there a way on how to do it ? please
Copy link to clipboard
Copied
In what way are they different, exactly?
Copy link to clipboard
Copied
@try67 If i upload image on the Image Field created using Prepare Form it is not reflecting on the field created using above Javascript.
Both the Image Fields are created on the same PDF with same field name
Copy link to clipboard
Copied
That's because the built-in Image field's action is always event.target.buttonImportIcon(), which only uploads the image to the field you clicked on, as we saw earlier.
Copy link to clipboard
Copied
Thank you for letting know. @try67
Is there a way if we can change the action on the built-up Image field's to match the one that is created using Java script


-
- 1
- 2