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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Acrobat

From novice to tech pro — start learning today.