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

How can I create a text field by using javascript?

Explorer ,
Mar 16, 2018 Mar 16, 2018

Question in the title. I think it is possible but apparently I'm too unable to do it

TOPICS
PDF forms
16.9K
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
1 ACCEPTED SOLUTION
Community Expert ,
Mar 16, 2018 Mar 16, 2018

This code will add a 100x100pt text field (called "Text1") at the lower left corner of the first page of the file:

this.addField("Text1", "text", 0, [0,100,100,0]);

View solution in original post

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
LEGEND ,
Mar 16, 2018 Mar 16, 2018

Please post the code you are using, and any error message from the console.

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 ,
Mar 16, 2018 Mar 16, 2018

Fields can be created using the addField method of the Document object. How to use it is documented in the Acrobat JS API Reference.

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
Explorer ,
Mar 16, 2018 Mar 16, 2018

Could you please give me a most easy example of addField with all the necessary parameters? In the API there's just some advanced example I don't really get. Sorry, pretty new on 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
Community Expert ,
Mar 16, 2018 Mar 16, 2018

This code will add a 100x100pt text field (called "Text1") at the lower left corner of the first page of the file:

this.addField("Text1", "text", 0, [0,100,100,0]);

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
Guest
Jan 23, 2020 Jan 23, 2020

Hello. I have had mixed success with locating the text box. When I first set up the scripting into an Action, I was able to get the proper placement. The next time I launched Acrobat and ran the Action, the horizontal (side to side) is fine, but the vertical is wrong, with the top of box appearing at 8.5 inches up from the bottom. If I slightly increase the uly value, the text starts vanishing (as if appearing off the top of the page).  I'm marking thousands of mulitple page documents with a Printed On date, so this has to work reliably.

// from left edge of letter sized page, left side of box

var ulx = 85;

// up from bottom of page, top of box (top edge of page)

var uly = 792;

// from left edge of page, right side of box (box is now 203 pt width)

var lrx = 288;

// up from bottom of page, bottom of box (box is now 22 pt tall)

var lry = 772;

for (var p=0; p<this.numPages; p++)

{

  var bf = this.addField("watermark", "text", p, [ulx,uly,lrx,lry]);

  bf.borderStyle = border.s;

  bf.lineWidth = 2;

  bf.textColor = color.black;

  bf.textFont = "Arial";

  bf.textSize = 10;

  bf.display = display.noview;

}

/* print Time and Date */

this.seAction("WillPrint", "var f = this.getField(\"watermark"\); f.value = util.printd(\"m/d/yyyy HH;MM\", new Date());");

 

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 ,
Jan 23, 2020 Jan 23, 2020
LATEST

The script needs to calculate the position in order to handle different page coordinates. 

Here's an article on this topic:

https://www.pdfscripting.com/public/PDF-Page-Coordinates.cfm?sd=40

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Mar 16, 2018 Mar 16, 2018

So it works like this.addField("Name", "type", pagenumber, [?,height,width,?])

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 ,
Mar 16, 2018 Mar 16, 2018

Not quite regarding the last parameter. From the API Reference:

oCoords : An array of four numbers in rotated user space that specifies the size and placement

of the form field. These four numbers are the coordinates of the bounding rectangle,

in the following order: upper-left x, upper-left y, lower-right x and lower-right y. See

also the Field object rect property.

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
Explorer ,
Mar 16, 2018 Mar 16, 2018

How come doesn't it work with a button? If I use this.addField("Text1", "button", 0, [0,100,100,0]), nothing happens

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 ,
Mar 16, 2018 Mar 16, 2018

Use a other name for the button.

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
Explorer ,
Mar 19, 2018 Mar 19, 2018

var f = this.addField("Textbox1", "text", 0, [0,792,100,692]);

f.borderStyle = border.m;

f.value = "Hello world!";

f.textSize = 12;

f.textfont = font.Arial;

f.textColor = color.black;

Why doesn't this put "Hello world!" into my textfield?

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 ,
Mar 19, 2018 Mar 19, 2018

You have several errors in your code. Check the JS Console when you run it...

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 ,
Mar 16, 2018 Mar 16, 2018

You can't have two fields of different types and the same name, so either delete the text field with that name, or change the name of the button you're trying to add.

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