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

Get pageNum of a OCG

Engaged ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

I am trying to identify what page number a certain layer is on. So I am getting all the OCGs then comparing for 2 specific layer names.

"No Color" and "651 Highways"

I am trying to identify what page number these reside on in the document and output it to the console.

Here is my code that I am running from the document level. It works with the exception of this.pageNum which obviously gives me the current page...not the page that the particular layers reside on.

var ocgArray = this.getOCGs();

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

if (ocgArray.name == "No Color") {

console.println("Found " + ocgArray.name + "\nPage number: " + this.pageNum);

}

if (ocgArray.name == "651 (Highways)") {

console.println("Found " + ocgArray.name + "\nPage number: " + this.pageNum);

}

}

On a side note....I will be searching for a pretty long list of layer names.... would this be better to create an array of layer names to search against?

Any help would be much appreciated!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

669

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 , Oct 09, 2018 Oct 09, 2018

The only relationship between the page number and a particular OCG is the "getOCGs()" function. Sooo, the only way to get the page related to a particular OCG is to use this function.

For example, this code prints out all OCGs on a per page basis.

for(pg=0;pg<this.numPages;pg++)

{

  console.println("Page# " + pg);

  console.println("\tOCGs: " + this.getOCGs(pg).toString());

}

Votes

Translate

Translate
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

You can specify the page as parameter of the method getOCGs.

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
Engaged ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

Using the nPage parameter?

But I don't know what page the layers will reside on....it will change from document to document.

So sometimes it may exist on page 4 sometimes on 2.... sometimes not at all.

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
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

Yes, this parameter.

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
Engaged ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

Ok. I don't understand the syntax that I need to show me what page number the specific OCG is on....

var ocgArray = this.getOCGs(nPage);

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

if (ocgArray.name == "No Color") {

console.println("Found " + ocgArray.name + "\nPage number: " + ocgArray.nPage);

}

if (ocgArray.name == "651 (Highways)") {

console.println("Found " + ocgArray.name + "\nPage number: " + ocgArray.nPage);

}

}

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
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

The page number is nPage. A ocg has no property named nPage.

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
Engaged ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

I see what you are saying. nPage returns the page number for the ocgs....when called getOCGs(nPage)

So can you tell me how to output that page number for the layer into the console?

  1. if (ocgArray.name == "No Color") {
  2. console.println("Found " + ocgArray.name + "\nPage number: " + WHAT GOES HERE TO SHOW "No Color" IS ON PAGE NUMBER ______ );
  3. }

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
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

You can use this:

console.println(nPage);

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
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

The only relationship between the page number and a particular OCG is the "getOCGs()" function. Sooo, the only way to get the page related to a particular OCG is to use this function.

For example, this code prints out all OCGs on a per page basis.

for(pg=0;pg<this.numPages;pg++)

{

  console.println("Page# " + pg);

  console.println("\tOCGs: " + this.getOCGs(pg).toString());

}

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
LEGEND ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

An OCG is global. It might be referred to on many pages.

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
Community Expert ,
Oct 09, 2018 Oct 09, 2018

Copy link to clipboard

Copied

I'm going to add a little context. First, you can't do directly what you are trying to do though Thom's answer will get you there. An OCG is an Optional Content Group. The OCGs are defined at the document level and sets of content on one or more pages can be set to belong to one or more groups. The nPage parameter in this.getOCGs(nPage) will return an array of OCG objects for the content on that page that is marked to belong to any group. Therefore, the only way to determine which pages have content hat belongs to a particular group is to loop through all the pages. If you know that a particular OCG is referenced by only one page, this isn't a big deal but as you state in your question, you've got a lot of layers and I'm guessing long files.

I suggest you write your code to build an array of arrays where each element of the outer array corresponds to a page number and each element of that array is an array of OCG names that are on that particular page. Then process the array for whatever actions you need to take rather than what you are doing now. It'll be a lot faster. 

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
Engaged ,
Oct 10, 2018 Oct 10, 2018

Copy link to clipboard

Copied

LATEST

Thank you all for the help and guidance. I marked Thom's answer as correct since it will point me to making the connection I need. Thank you for the explanation and feedback Joel Geraci​... That was very helpful as well!

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