Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1095
  • Last Modified:

Javascript to add text annotations to all pages.

I Need help to write a JavaScript program that asks for a log number and then creates the annotations to be put on all subsequent pages all in the same location.
0
andywheaton
Asked:
andywheaton
  • 4
  • 4
1 Solution
 
Karl Heinz KremerCommented:
I am swamped with work right now. How urgent is this? I can provide a solution over the weekend.
0
 
andywheatonAuthor Commented:
I originally asked this question in March to no avail. I ended up quitting EE over the lack of response to my only question I had posed. I ended up rejoining after it solved a different problem for me. So I thought I would repose the question. Lo and behold you answered. So no there is no big hurry on this. Again thanks for your help.
0
 
Karl Heinz KremerCommented:
OK, I'm back. I see that you posted your first question in a different TA - I don't hang out there ;)

Acrobat allows you to create JavaScript files that get executed when you start the application. We will make use of that feature with the following script - to add a menu item to Acrobat's "Document" menu. Create a new text file (use Notepad for that) in c:\Program Files\Acrobat 7.0\Acrobat\JavaScripts with the name "AddLogNumber.js" with the following content:

var dialog1 = {
    initialize: function (dialog) {
        this.logNumber = -1;
    },
    commit:function (dialog) { // called when OK pressed
        var results = dialog.store();
        this.logNumber = results["fnum"];
    },
    description:
    {
        name: "Log Number (2)", // dialog title
        align_children: "align_left",
        elements:
        [
                 {
                    type: "view",
                    align_children: "align_left",
                alignment: "align_fill",
                    elements:
                    [
                    {
                        type: "static_text",
                        name: "Log Number: "
                    },
                    {
                        item_id: "fnum",
                        type: "edit_text",
                  width: 80,
                    },
                   
                    {
                        type: "ok_cancel",
                        ok_name: "Ok",
                        cancel_name: "Cancel"
                     }
                     ]
                 }
          ]
   }
};

function addTextField(logNumber)
{
    // add the text field to every page
    for (var i=0; i<this.numPages; i++)
    {
        var annot = this.addAnnot(
        {
            page: i,
            type: "FreeText",
            rect: [36, 36, 108, 72],    // location and size of text field in points
            contents: logNumber,
        });
    }
}


function AddTextField()
{
    // create the user interface
    if (app.execDialog(dialog1) == "ok")
    {
       addTextField(dialog1.logNumber);
    }
}


// add the menu item
app.addMenuItem({
     cName: "JSAddTextField",
     cUser: "Add Text Field",
     cParent: "Document",
     cExec: "AddTextField()",
     cEnable: "event.rc = (event.target != null);",
     nPos: 0
});

// end of script

You can adjust the size and location of the text field by changing the "rect" data. The information is stored in points (72 points are one inch), and given in two coordinates: lower left and upper right corner.

Let me know if you run into any problems.
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
andywheatonAuthor Commented:
I have tried the above twice. When I open a document indeed there is a new menu item under Document named "Add Text Field". When I select it I get a dialog box asking me for The "Log Number". But when I type in something and hit enter nothing appears to happen. Any Ideas?
0
 
Karl Heinz KremerCommented:
Open the JavaScript console (Ctrl-J) and see any errors are reported.
0
 
andywheatonAuthor Commented:
I did not see any errors. This is what was there.

Acrobat Database Connectivity Built-in Functions Version 7.0
Acrobat EScript Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Functions Version 7.0
Acrobat Annotations / Collaboration Built-in Wizard Functions Version 7.0
Acrobat Multimedia Version 7.0
Acrobat SOAP 7.0
0
 
Karl Heinz KremerCommented:
That's odd... I developed the script on a Mac with Acrobat 7 Pro - and it worked. I just tried it on a Windows machine with the same version of Acrobat, and it fails here too.

I changed the script a little. Try this (it works on my Windows machine):

var dialog1 = {
    initialize: function (dialog) {
        this.logNumber = -1;
    },
    commit:function (dialog) { // called when OK pressed
        var results = dialog.store();
        this.logNumber = results["fnum"];
    },
    description:
    {
        name: "Log Number", // dialog title
        align_children: "align_left",
        elements:
        [
                 {
                    type: "view",
                    align_children: "align_left",
              alignment: "align_fill",
                    elements:
                    [
                    {
                        type: "static_text",
                        name: "Log Number: "
                    },
                    {
                        item_id: "fnum",
                        type: "edit_text",
               width: 80,
                    },
                   
                    {
                        type: "ok_cancel",
                        ok_name: "Ok",
                        cancel_name: "Cancel"
                     }
                     ]
                 }
          ]
   }
};

function processPages(doc, logNumber)
{
    // add the text field to every page
    var n = doc.numPages;
    for (var i=0; i<n; i++)
    {
        var annot = doc.addAnnot(
        {
            page: i,
            type: "FreeText",
            rect: [36, 36, 108, 72],    // location and size of text field in points
            contents: logNumber,
        });
    }
}


function AddTextField(doc)
{
    // create the user interface
    if (app.execDialog(dialog1) == "ok")
    {
       processPages(doc, dialog1.logNumber);
    }
}


// add the menu item
app.addMenuItem({
     cName: "JSAddTextField",
     cUser: "Add Text Field",
     cParent: "Document",
     cExec: "AddTextField(this)",
     cEnable: "event.rc = (event.target != null);",
     nPos: 0
});

// end of script
0
 
andywheatonAuthor Commented:
WOW that is great and it will save me a lot of work. Thanks for your help
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now