Solved

Javascript to add text annotations to all pages.

Posted on 2006-06-29
8
909 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2010 and fillable PDF forms 4 83
PDF files Slow openning 8 97
Select application to open PDF in IE 3 55
adobe acrobat scaning 9 64
Acrobat’s JavaScript is a great tool to extend the application, or to automate recurring tasks. There are several ways a JavaScript can be added to the application or a document (e.g. folder level scripts, validation scripts, event handling scripts,…
Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
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 …
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

825 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