Solved

Javascript to add text annotations to all pages.

Posted on 2006-06-29
8
876 Views
Last Modified: 2008-02-26
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
Comment
Question by:andywheaton
  • 4
  • 4
8 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 17018628
I am swamped with work right now. How urgent is this? I can provide a solution over the weekend.
0
 

Author Comment

by:andywheaton
ID: 17018728
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 17031433
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
 

Author Comment

by:andywheaton
ID: 17044788
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 17044832
Open the JavaScript console (Ctrl-J) and see any errors are reported.
0
 

Author Comment

by:andywheaton
ID: 17044874
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
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 17045598
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
 

Author Comment

by:andywheaton
ID: 17045668
WOW that is great and it will save me a lot of work. Thanks for your help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
In this third video of the Xpdf series, we discuss and demonstrate the PDFtoText utility, which converts PDF files into plain text files. Download and install the software.: You may have already downloaded and installed the Xpdf tools while watching…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now