Skip to main content
Participant
July 5, 2021
Answered

TypeError: this.addAnnot is not a function

  • July 5, 2021
  • 1 reply
  • 1987 views

I am trying to add a FreeText annotation to a PDF programmatically using JavaScript. When I try to call a function specifying the FreeText annotation directly from the menuItem, everything works as expected. When I try to call the FreeText annotation from another function, I get an error "TypeError: this.addAnnot is not a function". I am not sure what is wrong. I have included my code below. Your help is much appreciated.

Code:

app.addSubMenu({cName: "Tech Services"cParent: "Edit"});
app.addMenuItem({cName: "Add SO Info"cParent: "Tech Services"cExec: "addSalesOrderInfo();"});

function addSalesOrderInfo() {
   addSerialNumber();
   //addSalesOrderNumber();
   //addEhProjectNumber();
   //addNosetype();
   //addNumberOfUnits();
}

// add serial number textbox
function addSerialNumber() {
   
   // get serial number(s)
   var snValue = app.response({
      cQuestion: "Enter Serial Number(s)",
      cTitle: "SERIAL NUMBER ENTRY",
      cDefault: "N/A",
      cLabel: "Serial Number(s):"
   });
   
   // create text box
   var sn = this.addAnnot({
      page: 0,
      type: "FreeText",
      rect: [672,355,672+7,355+75],
      name: "SerialNo",
      author: "Admin",
   });

   // set properties of textbox
   var spans = new Array();
   spans[0] = new Object();
   spans[0].text = snValue;
   spans[0].textColor = color.black;
   spans[0].textFont = "Arial";
   spans[0].textSize = 8;
   spans[0].alignment = "center";

   // assign properties to rich contents of textbox
   sn.richContents = spans;   
}
This topic has been closed for replies.
Correct answer Test Screen Name

The meaning of "this" changes so never use it in a function. Instead pass the target field as a parameter. 

1 reply

Test Screen NameCorrect answer
Legend
July 5, 2021

The meaning of "this" changes so never use it in a function. Instead pass the target field as a parameter. 

Participant
July 5, 2021

Thank you for the help. I changed the code to:

function addSalesOrderInfo() {
   addSerialNumber(this);
   //addSalesOrderNumber();
   //addEhProjectNumber();
   //addNosetype();
   //addNumberOfUnits();
}

// add serial number textbox
function addSerialNumber(target) {
   
   // get serial number(s)
   var snValue = app.response({
      cQuestion: "Enter Serial Number(s)",
      cTitle: "SERIAL NUMBER ENTRY",
      cDefault: "N/A",
      cLabel: "Serial Number(s):"
   });
   
   // create text box
   var sn = target.addAnnot({
      page: 0,
      type: "FreeText",
      rect: [672,355,672+7,355+75],
      name: "SerialNo",
      author: "Admin",
   });

   // set properties of textbox
   var spans = new Array();
   spans[0] = new Object();
   spans[0].text = snValue;
   spans[0].textColor = color.black;
   spans[0].textFont = "Arial";
   spans[0].textSize = 8;
   spans[0].alignment = "center";

   // assign properties to rich contents of textbox
   sn.richContents = spans;   
}

Everything is now working as expected.