Solved

create bookmarks from string

Posted on 2012-03-16
14
676 Views
Last Modified: 2012-03-19
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?
0
Comment
Question by:ForLoop5
  • 8
  • 6
14 Comments
 
LVL 1

Author Comment

by:ForLoop5
ID: 37729766
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37730206
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
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 37730518
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37730534
The PDF is generated from a web service.  Then I am using Acrobat pro to run the java script on this.
0
 
LVL 1

Author Comment

by:ForLoop5
ID: 37731048
"Can you help me accomplish this with javascript for acrobat 9 pro?"
0
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 37731097
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37731250
I will create a generic pdf with the string and numbers we are looking to search for.  
Here it is.
example.pdf
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 37731360
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
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 37731485
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37732759
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37737708
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
 
LVL 9

Accepted Solution

by:
Shaun McNicholas earned 500 total points
ID: 37737977
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
 
LVL 9

Expert Comment

by:Shaun McNicholas
ID: 37737994
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
 
LVL 1

Author Comment

by:ForLoop5
ID: 37739357
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe In a previous Experts Exchange article, How To Rename-Move a Batch of PDF Files Based on Contents of the Files (http://www.experts-exchan…
PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/). It is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) and businesses (http:/…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…
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…

929 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

11 Experts available now in Live!

Get 1:1 Help Now