• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Split Document by Bookmarks

New Here ,
Jan 11, 2019 Jan 11, 2019

Copy link to clipboard

Copied

Hello,

I'm trying to create a script that splits a document at each bookmark, including any nested bookmarks, and naming the resulting files after that bookmark name. I've got the name and first page right, but the end page of the document is proving to be an issue for me. My code is as follows:

var bkmN=[];

var bkmS=[];

var bkmE=[];

function getBkmInfo(bkm, nLevel)

{ var s="";

  for(var i=1; i<nLevel; i++)

  { s+="-";

  }

  bkmN.push(s+bkm.name);

  // START PAGE ==============

  // =========================

  bkm.execute();

  bkmS.push(this.pageNum);

 

  // END PAGE ================

  // =========================

  bkmE.push(this.pageNum-1);

  // =========================

  if (bkm.children != null)

  { for(var i=0; i<bkm.children.length; i++)

    { getBkmInfo(bkm.children, nLevel+1);

    }

  }

}

getBkmInfo(this.bookmarkRoot, 0);

bkmN.splice(0, 1); // Removes bookmarkRoot info

bkmS.splice(0, 1); // Removes bookmarkRoot info

bkmE.splice(0, 1); // Removes bookmarkRoot info

console.clear();

for(var i=0; i<bkmN.length; i++)

{ console.println((i<10 ? "0"+i : i) + ") " + bkmN +

                                     " | " + (bkmS+1) +

                                     " - " + (bkmE+1)

  );

}

this.pageNum=0;

The end page for a document should be the start page of the next bookmark, minus one, but that leaves problems with the last page of the last bookmark. The code I have gives me a correct end page for every file/bookmark except for the first bookmark of each nested section, and the very last bookmark of the entire document (regardless of if it's nested or not).

Any help would be appreciated. I'm a little rusty on my javascript, so I'm sure there is an easy way to do this.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

519

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 13, 2019 Jan 13, 2019

The index for the bookmark end array is always one behind, because it is set at the same as the start. So index (1) is really the end for the start 0 (after removal of the root). Also, the last page of the document is the end page for the last bookmark. The solution is to remove the first entry of the end array and push the last page number on to the end of the end array.

Votes

Translate

Translate
Community Expert ,
Jan 13, 2019 Jan 13, 2019

Copy link to clipboard

Copied

LATEST

The index for the bookmark end array is always one behind, because it is set at the same as the start. So index (1) is really the end for the start 0 (after removal of the root). Also, the last page of the document is the end page for the last bookmark. The solution is to remove the first entry of the end array and push the last page number on to the end of the end array.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines