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

Help with loop for pages and ocg

Engaged ,
Oct 18, 2018 Oct 18, 2018

Copy link to clipboard

Copied

I am trying to create a bookmark with a JavaScript function attached to it. I have done that. It is under a parent bookmark called Schematic Tools.

My problem is that the iterations of my for loop are off. I'm not sure what I am doing wrong. The layers I am looking for will ONLY exist on odd page numbers (odd being based on the page 1 is page[0] so they are on pages 2, 4, 6, 8, etc..

var onPage = 1;

var usablePages = [];

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

    if (onPage <= this.numPages) {

        usablePages.push((onPage + 1));

        onPage += 2;

    }

}

// Array of all of the layers

var ocgArray = this.getOCGs();

// Create a header bookmark for all isolation tools to go under

this.bookmarkRoot.createChild("Schematic Tools");

var headerBookmark = this.bookmarkRoot.children[0];

headerBookmark.style = 3;

// Alphabetize the layers

ocgArray.sort();

for (var z = 0; z < usablePages.length; z++) {

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

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

            headerBookmark.createChild({

                cName: ocgArray.name,

                cExpr: "noColor();",

                nIndex: i

            });

            console.println("Page #: " + usablePages + "   Layer: " + ocgArray.name);

        }

        if (ocgArray.name == "Black (Ground)") {

            headerBookmark.createChild({

                cName: ocgArray.name,

                cExpr: "ground();",

                nIndex: i

            });

            console.println("Page #: " + usablePages + "   Layer: " + ocgArray.name);

        }

        if (ocgArray.name == "032 (Red) (Battery Power)") {

            headerBookmark.createChild({

                cName: ocgArray.name,

                cExpr: "zeroThreeTwo();",

                nIndex: i

            });

            console.println("Page #: " + usablePages + "   Layer: " + ocgArray.name);

        }

        if (ocgArray.name == "640 (Blue) (Key Power)") {

            headerBookmark.createChild({

                cName: ocgArray.name,

                cExpr: "sixFourZero();",

                nIndex: i

            });

            console.println("Page #: " + usablePages + "   Layer: " + ocgArray.name);

        }

        if (ocgArray.name == "021 (Orange) (Starting)") {

            headerBookmark.createChild({

                cName: ocgArray.name,

                cExpr: "zeroTwoOne();",

                nIndex: i

            });

            console.println("Page #: " + usablePages + "   Layer: " + ocgArray.name);

        }

    }

}

Here is the output to the console I am getting:

Page #: 2   Layer: 021 (Orange) (Starting)

Page #: 2   Layer: 032 (Red) (Battery Power)

Page #: 2   Layer: 032 (Red) (Battery Power)

Page #: 2   Layer: 640 (Blue) (Key Power)

Page #: 2   Layer: 640 (Blue) (Key Power)

Page #: 2   Layer: Black (Ground)

Page #: 2   Layer: Black (Ground)

Page #: 2   Layer: No Color

Page #: 2   Layer: No Color

Page #: 4   Layer: 021 (Orange) (Starting)

Page #: 4   Layer: 032 (Red) (Battery Power)

Page #: 4   Layer: 032 (Red) (Battery Power)

Page #: 4   Layer: 640 (Blue) (Key Power)

Page #: 4   Layer: 640 (Blue) (Key Power)

Page #: 4   Layer: Black (Ground)

Page #: 4   Layer: Black (Ground)

Page #: 4   Layer: No Color

Page #: 4   Layer: No Color

I know for a fact that this is what the CORRECT output SHOULD be:

Page #: 2   Layer: 021 (Orange) (Starting)

Page #: 2   Layer: 032 (Red) (Battery Power)

Page #: 2   Layer: 640 (Blue) (Key Power)

Page #: 2   Layer: Black (Ground)

Page #: 2   Layer: No Color

Page #: 4   Layer: 021 (Orange) (Starting)

Page #: 4   Layer: 032 (Red) (Battery Power)

Page #: 4   Layer: 640 (Blue) (Key Power)

Page #: 4   Layer: Black (Ground)

Page #: 4   Layer: No Color

Here are the bookmarks that get created:

What I would love ideally is if it would break them up under two parent headers

Schematic Tools - Page 1

021 (Orange) (Starting)

032 (Red) (Battery Power)

640 (Blue) (Key Power)

Black (Ground)

No Color

Schematic Tools - Page 2

021 (Orange) (Starting)

032 (Red) (Battery Power)

640 (Blue) (Key Power)

Black (Ground)

No Color

Sorry for all the info. Any help would be greatly appreciated!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

540

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

You don't need an array for the page numbers you want to process, first of all. Get rid of it.

Your mistake is that you've defined the ocgs array before your pages loop, so it simply contains all the ocgs in the entire file.

You should do it like this, instead:

for (var p = 0; p < this.numPages; p+=2) {

    var ocgArray = this.getOCGs(p);

    // insert rest the of ocg-related code here

}

Votes

Translate

Translate
Community Expert ,
Oct 18, 2018 Oct 18, 2018

Copy link to clipboard

Copied

You don't need an array for the page numbers you want to process, first of all. Get rid of it.

Your mistake is that you've defined the ocgs array before your pages loop, so it simply contains all the ocgs in the entire file.

You should do it like this, instead:

for (var p = 0; p < this.numPages; p+=2) {

    var ocgArray = this.getOCGs(p);

    // insert rest the of ocg-related code here

}

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

Copy link to clipboard

Copied

Perfect! Thank you try67​! I appreciate the help and explanation!

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

Copy link to clipboard

Copied

try67​ How can I make the bookmarks create in reverse order? So in other words it is currently making bookmark Schematic Tools - Volume 1 first....then Schematic Tools - Volume 2. The bookmarks that correspond with them are correct... I just want to have Schematic Tools - Volume 1 on top.

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

Copy link to clipboard

Copied

LATEST

Change the for-loop that iterates over the ocgs array to run the end of the array to its start, like this:

for (var i =ocgArray.length-1; i>=0; i--) {

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