4 Replies Latest reply on Dec 19, 2011 10:57 AM by Muthuraj Durairaj

    Convert all annotation into GotoPage links javascript -- Need help

    Muthuraj Durairaj

      Hi All,

       

      I have a large document and my requirement is to find the a set of words and add gotopage link to a particular page (index page). I recently got a action file from Acrobat users group (http://acrobatusers.com/content/find-highlight-words-and-phrases) which find and highlight a set of words using Acrobat action.

       

      This action consists of "search and Remove Text" action which will search and redact the selected text and a javascript to add yellow color highlight.

       

      I have modified the javascript associated with this action into the following so that the script will find all redactions made and convert them into the Gotopage links and destroy the annotations after creating the links.

       

      I tried these steps in the following code:

      1. Find the annotations
      2. Get the quad value
      3. Setlink to the particular page
      4. Destroy the redact annotations

       

      I am new to javascript and I have read the javascript reference as much I could and tried as much as possible. But this script is not working and I could not identify what is wrong in this.

       

      Please help me to complete this code.

       

       

      for (var p = 0; p < this.numPages; p++)
      {
      //var numWords = this.getPageNumWords(p);
      var numWords = this.getAnnots(p,{sortBy:"Author"});
      for (var i=0; i<numWords.length; i++)
      {
      var ckWord = this.getPageNthWord(p, i, true);
      if ( ckWord.type == "Redact")
      {
      var q = this.getPageNthWordQuads(p, i);
      // Convert quads in default user space to rotated
      // User space used by Links.
      m = (new Matrix2D).fromRotated(this,p);
      mInv = m.invert()
      r = mInv.transform(q)
      r=r.toString()
      r = r.split(",");
      l = addLink(p, [r[4], r[5], r[2], r[3]]);
      l.borderColor = color.red
      l.borderWidth = 0
      l.setAction.("this.pageNum = 243");
      }
      }
      }
      var annots = this.getAnnots();
      for (var i = 0; i < annots.length; i++)
      if (annots[i].type == "Redact") annots[i].destroy();
      
        • 1. Re: Convert all annotation into GotoPage links javascript -- Need help
          George_Johnson MVP & Adobe Community Professional

          You script is confused by dealing with the redaction annotation stuff. If you're not using them, there is no need to create them in the first place. Instead, search for the words and add the link around them. It's easy to locate a single word, but more diffucult for multiple words for a number of reasons, the most significant being if the the words are not all on the same line. It's also possible to have text rotated at an arbitrary angle relative to the page boundaries, and link annotations cannot be rotated, but this is often not an issue.

           

          The addLink documentation includes a sample script that does what you want for a single word: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.437.html

           

          You would have to modify the sample code to search for a string of words and generate the appropriate rect (oCoords) for the link.

          1 person found this helpful
          • 2. Re: Convert all annotation into GotoPage links javascript -- Need help
            Muthuraj Durairaj Level 1

            Hi George,

             

            I have tried the code sample you have provided. It is working fine. I just tried to make it as multiple words finder. I have modified this code to search only on one page (in bold font), rather than searching for entire 700 pages of document which is taking lot of time to complete. And, modified the setaction comand to goto a particular page. Please check and let me know if this is the correct way? Thanks a lot for your help.

             

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

            for (var p = 0; p < "this.pageNum = 25"; p++)

            {

            var numWords = this.getPageNumWords(p);

            for (var i=0; i<numWords; i++)

            {

            var ckWord = this.getPageNthWord(p, i, true);

            if ( ckWord == "Acrobat")

            {

            var q = this.getPageNthWordQuads(p, i);

            // Convert quads in default user space to rotated

            // User space used by Links.

            m = (new Matrix2D).fromRotated(this,p);

            mInv = m.invert()

            r = mInv.transform(q)

            r=r.toString()

            r = r.split(",");

            l = addLink(p, [r[4], r[5], r[2], r[3]]);

            l.borderColor = color.red

            l.borderWidth = 0

            //l.setAction("this.getURL('http://www.adobe.com/');");

            l.setAction("this.pageNum = 241");

            }

            }

            }

            • 3. Re: Convert all annotation into GotoPage links javascript -- Need help
              George_Johnson MVP & Adobe Community Professional

              If you want to use the same code, but search just a single page (25), change that line of code to:

               

              for (var p = 25; p < 26; p++)

               

              Otherwise you can remove the outer for loop entirely and set the variable p to 25 at the beginning of the script.

              • 4. Re: Convert all annotation into GotoPage links javascript -- Need help
                Muthuraj Durairaj Level 1

                Thanks a lot George. It works perfectly. I have added a input box to get the starting page and ending page and the search word using simple Javascript input box. I am the beginner in Acrobat Javascript and your help and appericiation made me so confident to experiment. Thanks again for your great help. Here is the code I am using. I have added this as menu item and it works perfectly in Acrobat 8 and throws "An internal error occured" in Acrobat X after it accepts the input values. But it works fine in Javascript console in Acrobat X. Please advise.

                 

                app.addMenuItem({  cName: "CreateLinks", cParent: "Edit", cExec: "CreateLinks();",  cEnable: "event.rc = (event.target != null);", nPos: 0 });

                 

                CreateLinks = app.trustedFunction(function()

                {

                 

                var pmin = app.response("Enter the starting page number");

                var pmax = app.response("Enter the ending page number");

                var FindWord = app.response("Enter the word to search");

                 

                //var p = 40;

                for (var p = pmin; p < pmax; p++)

                {

                var numWords = this.getPageNumWords(p);

                for (var i=0; i<numWords; i++)

                {

                var ckWord = this.getPageNthWord(p, i, true);

                if ( ckWord == FindWord)

                {

                var q = this.getPageNthWordQuads(p, i);

                // Convert quads in default user space to rotated

                // User space used by Links.

                m = (new Matrix2D).fromRotated(this,p);

                mInv = m.invert()

                r = mInv.transform(q)

                r=r.toString()

                r = r.split(",");

                l = addLink(p, [r[4], r[5], r[2], r[3]]);

                l.borderColor = color.red

                l.borderWidth = 0

                l.setAction("this.pageNum = 243");

                }

                }

                }

                app.alert({

                cMsg: "Links has been created for the given word",

                cTitle: "Linking Done"

                });

                 

                })