Link to home
Start Free TrialLog in
Avatar of christampa
christampa

asked on

Adobe Acrobat run headers/footers javascript execMenuItem

Hello, anyone know how to run the page headers/footers menu option in acrobat 6.0 and then add page numbers?  I know the code app.execMenuItem("[menuitem]"), does just that, but i cant find the one to do headers and footers.  Thanks.

Chris
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of christampa
christampa

ASKER

Great Thanks! Do you know how to change thetab to "footer" then add ageumbers toth right as "1 o n? Thanks

chris
It is not possible to do this with JavaScript. If you want to add a footer with JavaScript, you need to use free text annotations, construct your string and then place it according to the page size.
I was trying to use the built in headers/footers for the page numbers.  is there any way to set up a button or a menu to do this?  Thanks.
Put this script into the Acrobat JavaScripts directory (C:\Program Files\Adobe\Acrobat 6.0\Acrobat\Javascripts):

function numberPages()
{
      for (var i = 0; i < this.numPages; i++)
      {
            // get the crop box for the page
            var aRect = this.getPageBox("Media", i);
            var width = aRect[2] - aRect[0];
            var height = aRect[1] - aRect[3];

            // create the form fields for all pages
            var fieldPos = [width-36, height-36, width, height];
            var label = "PageNumber_" + i;
            var f = this.addField(label, "text", i, fieldPos);
            f.textSize = 12;
            f.textColor = color.blue;
            f.fillColor = color.transparent;
            f.textFont = font.HelvB;
            f.borderStyle = border.s;
            f.strokeColor = color.transparent;
            var pageNumber = i + " of " + (this.numPages + 1);
            f.value = i+1;      // Acrobat page numbers are zero based
      }
}

// add the menu item
app.addMenuItem({
      cName: "KHK_:JSNumberPagesMenu",
      cUser: "Page Numbers",
      cParent: "File",
      cExec: "numberPages()",
      cEnable: "event.rc = (event.target != null);",
      nPos: -1,
});


Once you restart Acrobat, you have a new entry in the Files menu to number your pages. You can adjust the position and the color.
The problem with that one is what happens when they add or delete pages that alreadyhave page numbers?  if they do this and rerun the script, it will just add another page number on top of the other one.  That was why the built in one was the best to use.?
Because you know what the field IDs are (PageNumber_xxx), you can therefore delete all these fields before you apply new page numbers.
how can u delete it? :(  code please?

also to put it on the bottom?  

I am sorry I am new to this and u seem like an expert
function removePageNumbers(doc)
{
      for (var j=doc.numFields; j > 0; j--)
      {
            var theName = doc.getNthFieldName(j-1);
            if (theName.substring(0, 11) == "PageNumber_")
            {
                  doc.removeField(theName);
            }
      }
}


function numberPages(doc)
{
      removePageNumbers(doc);

      for (var i = 0; i < doc.numPages; i++)
      {
            // get the crop box for the page
            var aRect = doc.getPageBox("Media", i);
            var width = aRect[2] - aRect[0];
            var height = aRect[1] - aRect[3];

            // create the form fields for all pages
            var fieldPos = [width-36, 0, width, 36];
            var label = "PageNumber_" + i;
            var f = doc.addField(label, "text", i, fieldPos);
            f.textSize = 12;
            f.textColor = color.blue;
            f.fillColor = color.transparent;
            f.textFont = font.HelvB;
            f.borderStyle = border.s;
            f.strokeColor = color.transparent;
            f.value = i+1;      // Acrobat page numbers are zero based
      }
}


// add the menu item
app.addMenuItem({
      cName: "KHK_:JSRemovePageNumbersMenu",
      cUser: "Remove Page Numbers",
      cParent: "File",
      cExec: "removePageNumbers(this)",
      cEnable: "event.rc = (event.target != null);",
      nPos: -1,
});

app.addMenuItem({
      cName: "KHK_:JSNumberPagesMenu",
      cUser: "Page Numbers",
      cParent: "File",
      cExec: "numberPages(this)",
      cEnable: "event.rc = (event.target != null);",
      nPos: -1,
});

You can modify the position of the page number by modifying this line:
            var fieldPos = [width-36, 0, width, 36];
It's four components are [distance from left edge, distance from bottom edge, width, height], and the position is specified in points (1/72 ").
you are the best!!! Thanks!

One last thing.... the page number of N isnt working.  Ie page 3 of 12

it puts correct page but doesnt total them

thanks

actually i fixed that.  

but when i add page numbers for a bunch of pages, it seems to move slightly on the page?  do you know why?  here is the completed code....is it cause the width is different depending on what page it is on? is there a way to set the width to a static width?  thanks

function removePageNumbers(doc)
{
     for (var j=doc.numFields; j > 0; j--)
     {
          var theName = doc.getNthFieldName(j-1);
          if (theName.substring(0, 11) == "PageNumber_")
          {
               doc.removeField(theName);
          }
     }
}


function numberPages(doc)
{
     removePageNumbers(doc);

     for (var i = 0; i < doc.numPages; i++)
     {
          // get the crop box for the page
          var aRect = doc.getPageBox("Media", i);
          var width = aRect[2] - aRect[0];
          var height = aRect[1] - aRect[3];

          // create the form fields for all pages
          var fieldPos = [width-55, 0, width, 32];
          var label = "PageNumber_" + i;
          var f = doc.addField(label, "text", i, fieldPos);
          f.textSize = 11;
          f.textColor = color.black;
          f.fillColor = color.transparent;
          f.textFont = font.HelvB;
          f.borderStyle = border.s;
          f.strokeColor = color.transparent;
          f.value = String((i+1) + " of " + this.numPages);     // Acrobat page numbers are zero based
     }
}


// add the menu item
app.addMenuItem({
     cName: "KHK_:JSRemovePageNumbersMenu",
     cUser: "Remove Page Numbers",
     cParent: "Advanced",
     cExec: "removePageNumbers(this)",
     cEnable: "event.rc = (event.target != null);",
     nPos: -1,
});

app.addMenuItem({
     cName: "KHK_:JSNumberPagesMenu",
     cUser: "Page Numbers",
     cParent: "Advanced",
     cExec: "numberPages(this)",
     cEnable: "event.rc = (event.target != null);",
     nPos: -1,
});
Sorry for the delay, but it looks like you figured it out. If you want to do more with JavaScript, you should take an Introduction into JavaScript and get an understanding of the basics. When you look at browser JavaScript and Acrobat JavaScript, you actually have two parts: The core language, which is the same for both the browser and Acrobat, and then the application specific extensions (e.g. how to deal with Web pages for the browser, and how to deal with PDF form fields in the case of Acrobat). The core language is the same (this is the basic syntax and the core libraries  - e.g. how to deal with strings). Once you have a good understanding of the core language, you should be able to just take the Acrobat JavaScript guide and start programming. Unfortunately the version for Acrobat 6 is no longer free, but you should still be able to get the Acrobat 5 version, which is good for probably 90% of what you want to do .

The position of the page number is calculated based on the width of the page (e.g. width - xxx). This means that if your pages have different sizes, the page number will also placed differently. You can change your formula so that it uses an absolute value (e.g. 8.25" from the left edge). So just change the line that defines the position and get rid of all "width" statements.
thank you for the help i appreciate it.  

if i have a document of 50 pages and I delete a page and try to redo the page numbers it gives me an error.

GeneralError: Operation failed.
Global.removeField:8:Menu KHK_:JSNumberPagesMenu:Exec

is there a way to do some error checking or something?

I just tried this with a 50 page document, applied page numbers, removed one page, and reapplied the page numbers. I did not get this error, and the original page numbers were correctly removed before the new ones were added.

Which version of Acrobat are you using (be as specific as possible, e.g. 6.0.1 or 5.0.5), which language?

I'm running the English Acrobat 6.0.2 Professional.
its english acrobat professional  6.0.0 5/19/2003

also do you know how to add a button in the toolbar for it?

how can I give you more points for answering my questions?
Please upgrade to at least 6.0.1 (this is a free download from Adobe's web site). You can try the upgrade function in the help menu. If this does not work, please download teh upgrade from http://www.adobe.com/support/downloads/product.jsp?product=1&platform=Windows (you need to first go to 6.0.1 before you can upgrade to 6.0.2).

I know how to add a tool button with JavaScript, but have not done it in quite some time. I need to read up and experiment.

500 points is the maximum for one question. You can e.g. open a new question for the tool button.

it wont let me update acrobat :(

it jsut hangs at the "copying files"  with the installer and then says "installer not responding"

are you familiar with this errors?  thanks.
No, I'm not familiar with this problem. Did you download the installer, or did you use the "Update..." menu item in the "Help" menu?

Is your computer a "normal" machine, or is it maintained by your IT department? In the latter case, they may have locked it down, so that you cannot install any software.