3 Replies Latest reply on Nov 10, 2008 7:35 PM by (miniCooper)

    Creating a button inside PDF

      Hi,

      I'm trying to create a js file that creates two buttons which execute menu items.

      I have it working in the batch process but when I try to create a js file and add a menu item to execute the file, I get the following error message:

      this.addField is not a function
      24:Menu:Exec

      Here is my code:

      var inch = 72;

      function loadNavButtons()
      {
      try {
      AddButtons(0,3.8,6.85,0.25,0.25,"Next",">>","Next Slice","app.execMenuItem(\"KM2008:myNextImageSlice\");");
      AddButtons(0,4.1,6.85,0.25,0.25,"Prev","<<","Previous Slice","app.execMenuItem(\"KM2008:myPrevImageSlice\");");
      }

      catch (e) {
      app.alert("ERROR: Cannot execute these image slices");
      }
      }

      function AddButtons(nPageNum, x, y, width, height, strText, strCaption, strToolTip, strAction)
      {
      var aRect = [];
      aRect[0] += x * inch;
      aRect[1] -= y * inch;
      aRect[2] = aRect[0] + width * inch;
      aRect[3] = aRect[1] - height * inch;

      // Add the field "button"
      var f = this.addField(strText, "button", nPageNum, aRect);

      //var f = this.addField("test", "button", 0, [100, 472, 172, 400]);

      // Properties:
      f.setAction("MouseUp", strAction);
      f.userName = strToolTip;
      f.delay = true;
      f.borderStyle = border.s;
      f.highlight = "push";
      f.textSize = 0;
      f.textColor = color.black;
      f.strokeColor = color.blue;
      f.fillColor = color.white;
      f.buttonSetCaption(strCaption);
      f.delay = false;
      }

      app.addMenuItem({
      cName: "Next",
      cUser: "JS: Next",
      cParent: "Advanced",
      cExec: "loadNavButtons();",
      nPos: 0
      });

      Could someone please help me figure out this error? I'm not sure why the this.addField is not working. Do I need to include something in my js code?

      Thanks,
      Kristy
        • 1. Re: Creating a button inside PDF
          George_Johnson MVP & Adobe Community Professional
          The problem is "this" does not refer to the current document when the function is called. You should pass a reference to the current document as a parameter to the function and use it instead. For example:

          cExec: "loadNavButtons(this);",

          and then:

          function loadNavButtons(doc)
          {
          try {
          AddButtons(doc, 0,...

          and then:

          function AddButtons(doc, nPageNum, ...

          and finally:

          // Add the field "button"
          var f = doc.addField(strText, "button", nPageNum, aRect);

          George
          • 2. Re: Creating a button inside PDF
            Level 1
            Hi George,

            Thanks for all the helpful tips, but for somehow I still get the same error:

            doc.addField is not a function
            26:Menu:Exec

            I'm not sure why this is happening... the try never gets executed, it always goes into the catch and then another message box appears saying that an internal error has occured.

            Here's my revised code:

            var inch = 72;

            function loadNavButtons(doc)
            {
            try {
            AddButtons(doc, 0,3.8,6.85,0.25,0.25,"Next",">>","Next Slice","app.execMenuItem(\"KM2008:myNextImageSlice\");");
            AddButtons(0,4.1,6.85,0.25,0.25,"Prev","<<","Previous Slice","app.execMenuItem(\"KM2008:myPrevImageSlice\");");
            }

            catch (e) {
            app.alert("ERROR: Cannot execute these image slices");
            }
            }

            function AddButtons(doc, nPageNum, x, y, width, height, strText, strCaption, strToolTip, strAction)
            {
            var aRect = [];
            aRect[0] += x * inch;
            aRect[1] -= y * inch;
            aRect[2] = aRect[0] + width * inch;
            aRect[3] = aRect[1] - height * inch;

            // Add the field "button"
            var f = doc.addField(strText, "button", nPageNum, aRect);

            // Properties:
            f.setAction("MouseUp", strAction);
            f.userName = strToolTip;
            f.delay = true;
            f.borderStyle = border.s;
            f.highlight = "push";
            f.textSize = 0;
            f.textColor = color.black;
            f.strokeColor = color.blue;
            f.fillColor = color.white;
            f.buttonSetCaption(strCaption);
            f.delay = false;
            }

            app.addMenuItem({
            cName: "Next",
            cUser: "JS: Next",
            cParent: "Advanced",
            cExec: "loadNavButtons(this);",
            nPos: 0
            });

            Does anyone know how this issue can be resolved?

            Any help would be great!
            Thanks,
            Kristy
            • 3. Re: Creating a button inside PDF
              Level 1
              Alright,

              Thanks so much George for your help!

              I finally got it to create buttons. Turns out that I also had to change my rectangle array location from var aRect = []; to

              var aRect = doc.getPageBox( { nPage: nPageNum} );

              Thanks again!