Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

action item to create image field using javascript in adobe acrobat - PDF Forms

New Here ,
May 23, 2023 May 23, 2023

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

TOPICS
PDF forms
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2023 May 23, 2023

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;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2023 May 23, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2023 May 23, 2023

thank you for the guidance. 
i will work on it and getback

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2023 May 23, 2023

@try67  please help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2023 May 23, 2023

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;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 23, 2023 May 23, 2023

@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);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

Check the Javascript console for errors.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

- 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 24, 2023 May 24, 2023

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2023 May 24, 2023

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();");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 24, 2023 May 24, 2023

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

Are all image fields in the same document?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2023 May 25, 2023

Yes Images are in same document

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2023 May 25, 2023

@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();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

You need to use it as the string for the setAction method, like this:

firstButton.setAction("MouseUp", "this.getField(event.target.name).buttonImportIcon();");

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2023 May 25, 2023

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.. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

Try this:

Change the action of the manual created image field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 26, 2023 May 26, 2023

Thank you @Bernd Alheit 
is there a way on how to do it ? please

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2023 May 25, 2023

In what way are they different, exactly?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2023 May 25, 2023

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2023 May 26, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 26, 2023 May 26, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines