Script to create/name bookmark from text in the pdf

Jan 10, 2020

Copy link to clipboard

Copied

Hello,

 

I'm wondering how I can create a script that will create a bookmark and then name it with a value in the PDF file. The value is a randomized number that will have the same number of characters. So if the pdf is ten pages, I should have ten bookmarks named with that unique number.

 

After this is done my plan is to be able to split the pdf file based on the bookmark. Unless this can be scripted too but that's for another time.

 

Thanks for any help you can provide.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, How to

Views

1.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Script to create/name bookmark from text in the pdf

Jan 10, 2020

Copy link to clipboard

Copied

Hello,

 

I'm wondering how I can create a script that will create a bookmark and then name it with a value in the PDF file. The value is a randomized number that will have the same number of characters. So if the pdf is ten pages, I should have ten bookmarks named with that unique number.

 

After this is done my plan is to be able to split the pdf file based on the bookmark. Unless this can be scripted too but that's for another time.

 

Thanks for any help you can provide.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, How to

Views

1.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 10, 2020 1
Jan 10, 2020

Copy link to clipboard

Copied

With a script there are two ways to create a bookmark. 

1) use the "bookmark.createChild()" function. This creates a bookmark with a JavaScript Action. You can read about it here:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

You'll need to learn a bit about bookmark navigation. 

 

2) use the menu item "NewBookmark", which creates a new unnamed bookmark following the currently selected bookmark. The bookmark action is the destination of the current page view. While this method is the only way to create bookmarks with true destinations from a script, it is extremely problematic for two reasons. First, the exeMenuItem function is non-blocking, so you don't know when and if the bookmark is created. And second, this menu item is intended to be used from the user interface, so it places the keyboard focus into the currently created bookmark, making it impossible to rename that bookmark.  

 

 

Frankly, it might be much easier to just split the document. On what basis is the split.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2020 0
Jan 11, 2020

Copy link to clipboard

Copied

my plan is to be able to split the pdf file based on the bookmark

This can be automated with the Action wizard or by using "Split Multiple Files".

 

Capture_085.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 11, 2020 0
Jan 12, 2020

Copy link to clipboard

Copied

Hello;

 

I am looking to do the same. I have a large pdf every 2 pages is associated with a particular individual. I have ID numbers on each page that is unique to each individual, so I was looking to have a javascript to do the following:

 

1. Automatically set bookmarks to the position of the ID number and name the bookmark as the ID number

2. Split the pages  in correspondence to the unique id numer

3. Save each file separetly as the unique id number for file name

 

I have the javascript for renaming files as the unique id bookmark working, but automating creating bookmarks as the Unique ID I am struggling with

 

Any help would be appreciated

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 12, 2020 1
Jan 13, 2020

Copy link to clipboard

Copied

Nice work, sounds like you're further along than I. Would you be willing to provide your code for creating/naming the bookmark? This is the step I'm hung up on as my programming/scripting skills are not up to snuff.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2020 0
Jan 14, 2020

Copy link to clipboard

Copied

I think we are both hung up on the same step. I am also looking for a script to create bookmarks  ( i think it would be position on each page).

All I have is the initial code for sving the file as the content of a bookmark,

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2020 0
Jan 13, 2020

Copy link to clipboard

Copied

Yes, I know how that's done but I was hoping to automate this step after the naming of the bookmark. No big deal though because it's an easy process as you've displayed above.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2020 0
Jan 14, 2020

Copy link to clipboard

Copied

So is your ultimate goal to save each page of the PDF to a file unique name? 

This is much easier to do with a script than creating bookmarks. 

Use the "doc.extractPages()" function, which you can read about here:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2020 0
Jan 14, 2020

Copy link to clipboard

Copied

For my situation I need to split and save the file as the unique ID text on each page

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2020 0
Jan 14, 2020

Copy link to clipboard

Copied

Yes, this is my situation as well. 

 

I need to gather the unique ID from the page, then create a bookmark named as the unique ID. Only difference is each split pdf will be 1 page vs your 2.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2020 0
Jan 14, 2020

Copy link to clipboard

Copied

Where on the page is this unique ID located? Is it a fixed location? Is there a unique label that can be used to locate it? is the format of the ID number unique?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2020 0
Jan 16, 2020

Copy link to clipboard

Copied

1. For me, the Unique ID is in a fixed location.

2. Before the Unique ID is the label: "Employee Number:"  eg. Employee Number: 123456789

3. The unique ID is a somewhat random number, that is 9 characters long.

 

Your earlier comment about using the doc.extract function looks like it would be easier than creating the bookmarks. I'm just not sure how to capture that unique ID.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2020 0
Jan 16, 2020

Copy link to clipboard

Copied

You would need to use the getPageNthWord method and see if you could extract that text based on the text surrounding it. Or you could use getPageNthWordQuads if that text always appears at the same location on the page, but that's quite tricky to do.

And if you just want to use that text as the name of the extracted pages you don't really need to use bookmarks at all. You can do it directly with the value once you find it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2020 0
Jan 16, 2020

Copy link to clipboard

Copied

What try said 🙂

 

I use the getPageNthWordQuads function to find page words all the time. However, since you have a simple situation, I'd go with just finding the words. If you can guarentee that the ID number is always 9 digits, and there are no other 9 digit sequences on the form. Then it's pretty easy to just loop through all the words on the page until you find a pattern match. 

Like this:

 

var nWords = this.getPageNumWords(pageNum);

for(var i=0;i<nWords;i++)

{

      cWord = this.getPageNthWord(pageNum,i,true);

      if((cWord.length == 9) && /^\d+$/.test(cWord))

      {// Got ID

             break;

      }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2020 0
Jan 17, 2020

Copy link to clipboard

Copied

Ok, minor setback. There's another number on this page that can be 9 characters long.

 

Is there a command that I can use with your script above that would look for "Employee Number" and then take the value after that?

 

You guys are awesome, thanks for all the help so far.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2020 0
Jan 17, 2020

Copy link to clipboard

Copied

For myself also it is a set unique is in a set position.

I also my run into the same issue of other numbers matching, this is why I thought automating bookmarks would be the best way to split and save the file.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2020 0
Jan 18, 2020

Copy link to clipboard

Copied

When you find a match start looking at the words before it. If word i-1 is "Number" and i-2 is "Employee" then it's the number you're looking for.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 18, 2020 0
Jan 19, 2020

Copy link to clipboard

Copied

Well, I certainly appreciate all the input and shared knowledge, but I think for me it is easier to simply add the book marks and split the pages and save according to top level book mark.

 

I was really hoping to beable to automate adding the book marks.

 

Thank You

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2020 0
Jan 19, 2020

Copy link to clipboard

Copied

You can still do that, but the first step is identifying the numbers you're after...

 

If you're interested I could write this script for you, for a small fee. You can contact me privately via [try6767 at gmail.com] to discuss it further.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2020 0
Mar 12, 2020

Copy link to clipboard

Copied

For troubleshooting, how can I display the word that this script is gathering?

 

var nWords = this.getPageNumWords(pageNum);

for(var i=0;i<nWords;i++)

{

      cWord = this.getPageNthWord(pageNum,i,true);

      if((cWord.length == 9) && /^\d+$/.test(cWord))

      {// Got ID

             break;

      }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 12, 2020 0
Mar 12, 2020

Copy link to clipboard

Copied

console.println(cWord);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 12, 2020 1
Mar 12, 2020

Copy link to clipboard

Copied

Watch the Console Window tutorial here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 12, 2020 0
May 06, 2020

Copy link to clipboard

Copied

did you get your solution? I am in a similar situation.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 06, 2020 0
May 08, 2020

Copy link to clipboard

Copied

I didn't, but the advice from Thom and Try will get you on the right track.

 

The following code is a good start.

 

var nWords = this.getPageNumWords(pageNum);

for(var i=0;i<nWords;i++)

{

      cWord = this.getPageNthWord(pageNum,i,true);

      if((cWord.length == 9) && /^\d+$/.test(cWord))

      {// Got ID

             break;

      }

}

 

It was able to find the number that I needed it to and combined with a variation of the next bit of code, I was able to create/name a pdf with the employee's number. The problem I had was that it stopped after creating the document from the first page. I'm too much of a pigeon to have it work through the remaining pages so unfortunately, I dumped the project. 

 

  /* Extract pages to folder */

   // Regular expression used to acquire the base name of file

   var re = /\.pdf$/i;

   // filename is the base name of the file Acrobat is working on

   var filename = this.documentFileName.replace(re,"");

   try {for (var i = 0; i < this.numPages; i++)

         this.extractPages({

            nStart: i,

            cPath: "/F/temp/"+filename+"_" + i +".pdf"

         });         

   } catch (e) { console.println("Aborted: " + e) }

 

I hope this helps get you started.

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 08, 2020 0
Nuge93 LATEST
May 08, 2020

Copy link to clipboard

Copied

I see from your thread that you're looking to get the client's name. That will be more difficult than my scenario as the value I'm looking for is always a 9 digit number and there weren't any others on the page that matched the criteria.

 

Best of luck!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 08, 2020 0