create bookmarks from string

I have a 500 page pdf.  It currently has no bookmarks.  I would like to create a book mark on each page.  Each page has an id on it with this label "SASID:"     After the label there is a 10 digit # that follows. I would like to make the number on each page a bookmark.  

Can you help me accomplish this with javascript for acrobat 9 pro?
LVL 1
ForLoop5Asked:
Who is Participating?
 
Shaun McNicholasSenior Marketing TechnologistCommented:
Sweet - nice progress.
I'll see if I can finish it!
Very cool stuff.


HAHAHAHAH!!!!! I DID IT!!!!

I love figuring this kind of stuff out!
I'm such a geek! My wife would say...

// JavaScript Document
var root = this.bookmarkRoot;
var RegularExpression  =  new RegExp(/\n|\r|\t|\s/);

for (var i = 0; i < this.numPages; i++)
    {
        numWords = this.getPageNumWords(i);
        var PageText = "";
		var FoundSASID = 0;
        
		for (var j = 0; j < numWords; j++) {
            var word = this.getPageNthWord(i,j,false);
			PageText += word;

			var strMatches = PageText.match('SASID:');
        	if (strMatches != null) { 
				// Found text SASID: So reset the PageText to nothing and set the FOundSASID to 1
				FoundSASID = 1;
				PageText = "";
				}

			var newWord = PageText.match(RegularExpression);
			var foundSpace = PageText.match(' ');
			if (newWord != null || foundSpace != null) { 
				if (FoundSASID==1) {
					root.createChild(PageText,"this.pageNum=" + i, i); 
					FoundSASID = 0;
				} 
				if (FoundSASID==0) {
					PageText = "";
				}

				}
            }
    }

Open in new window

0
 
ForLoop5Author Commented:
I found this to create bookmarks of each page.

I am going to try an modify it to make each of the 10 digit #s the book marks. Any help would be appreciated.

/* Create bookmarks for each page in the document */
var root = this.bookmarkRoot;
try {

    for (var i = 0; i < this.numPages; i++)
       {
       root.createChild("Page " + (i+1), "this.pageNum=" + i, i);
       }
}
catch(e)
{
app.alert("Processing error: "+e)
}
0
 
ForLoop5Author Commented:
If we modify this for loop to search for  the string "SASID:**********"  on the page then remove the "SASID:" and create a variable called pageName and set it equal to the string **********  it should work to create bookmarks with the title of each SASID.  

var pageName = 0;
  for (var i = 0; i < this.numPages; i++)
       {
      //here we would need to creat the expression that searches for "SASID:*********" then removes "SASID:" and sets pageName = **********

       root.createChild(pageName);

       }




Anyone around?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Shaun McNicholasSenior Marketing TechnologistCommented:
You're generating this pdf through a web service or in Acrobat Pro or from where?
Is it being created from a text file or a database query or something?
If its being generated from a web page then what language are you writing in?
0
 
ForLoop5Author Commented:
The PDF is generated from a web service.  Then I am using Acrobat pro to run the java script on this.
0
 
ForLoop5Author Commented:
"Can you help me accomplish this with javascript for acrobat 9 pro?"
0
 
Shaun McNicholasSenior Marketing TechnologistCommented:
Sorry I'm not really an Acrobat expert. I've never even used javascript in Acrobat Pro before and I have CS5 not 9

I did take a look at your javascript and it does work as advertised in CS5, but it wouldn't do much in the way of accomplishing what you're trying to do.

If I was looking at something in the web I would get all the ID tags by using the document.getElementsByTagName('id')

Then loop through those tags grabbing the text you need - strip the SASID: and create the bookmark pointing to that tag. But again unless I had your document to experiment with I wouldn't be able to test anything like that myself to verify that its even possible to accomplish this in Acrobat.

Can you upload one of these documents. Maybe just a few pages of one of them, so I can download it and see if javascript in Acrobat functions the same way as it does in a browser?
0
 
ForLoop5Author Commented:
I will create a generic pdf with the string and numbers we are looking to search for.  
Here it is.
example.pdf
0
 
Shaun McNicholasSenior Marketing TechnologistCommented:
I may not be able to look at this in any kind of detail until Monday. So if anyone else in EE wants to take over please feel free. Otherwise I will get back to you as soon as I have some time to sit down and look into this. I'm curious myself about the JavaScript functionality in Acrobat. So I'll let you know what I can figure out as soon as I can.
0
 
Shaun McNicholasSenior Marketing TechnologistCommented:
Well I haven't figured everything out but this will get you started.

var root = this.bookmarkRoot;
// for (var i=0; i<3; i++)
// {
// root.createChild("Page"+(i+1),"this.pageNum="+i, i);
// }

for (var i = 0; i < this.numPages; i++)
    {
        numWords = this.getPageNumWords(i);
        var PageText = "";
        for (var j = 0; j < numWords; j++) {
            var word = this.getPageNthWord(i,j,false);
            PageText += word;
            }
        var strMatches = PageText.match('SASID:');
        if (strMatches == null) continue;
        // now output matches into report document
        for (j = 0; j < strMatches.length; j++) {
            root.createChild('SASID'+(i+1),"this.pageNum="+i, i);

            }
    }

Open in new window


You should be able to do a search for the string 'SASID:' and get just the text after that string and use that as the bookmark. But this will at least get you started.

I'm on my way out for diner. If I have time over the weekend I'll see if I can figure anything else out for you.
0
 
ForLoop5Author Commented:
I worked on this for a long while last night and couldnt get it.  

I was trying to modify your search string to include the 10 digits.  I tried adding \d 10 times to represent a numerical wildcard but that returned an error.  

I also tried to change the  root.createChild('SASID'+(i+1),"this.pageNum="+i, i);
to  root.createChild(strMatches);     in hopes that this would return the current 10 digit number as the bookmark.  

But I couldnt get it up and running.
0
 
ForLoop5Author Commented:
OK.  I have made a bunch of progress.  

This will do it all except remove the SASID: from before the numbers.  I still need to work on that.  


var root = this.bookmarkRoot;

for (var i = 0; i < this.numPages; i++)
    {
        numWords = this.getPageNumWords(i);
        var PageText = "";
         var RegularExpression  =  new RegExp(/SASID:\d+/g);
        for (var j = 0; j < numWords; j++) {
            var word = this.getPageNthWord(i,j,false);
            PageText += word;
            }
        var strMatches = PageText.match(RegularExpression);
        if (strMatches == null) continue;
        // now output matches into report document
      // app.alert(strMatches);
        for (j = 0; j < strMatches.length; j++) {

            root.createChild(strMatches);

            }
    }
0
 
Shaun McNicholasSenior Marketing TechnologistCommented:
I think the secret to the script is that the numWords is actually counting characters not words. So it's looping through every character... so the script may take a little while on a large document. Plus you can add text if you want before the link by just adding a text insert in front of the PageText

root.createChild('Link to this page marker: '+PageText,"this.pageNum=" + i, i);
0
 
ForLoop5Author Commented:
Maestropsm thanks for the excellent help!  This was also my first time using javascript in acrobat.

  It does create allot of possibilities for some cool and useful document processing in enterprise level networks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.