Highlighted

Javascript to toggle layer visibility doesn't work on pages created from page templates

Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

I have a RPG character sheet with a toggle switch for a layer visibility. It works great on the main document pages but when I add new pages from templates the script doesn't work on the new pages.
Im using this script to toggle the visibility:

var layers = this.getOCGs();
for(var i=0;i<this.layers.length;i++) {
    if(layers[i].name == "Extras") {
        layers[i].state = !(layers[i].state);
    }
} 

 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

I took a quick look at this. Page spawing works by copying all the page contents for the template page into a single XObject. Which makes sense. But it makes new copies of all the indirect objects, and most OCGs are added as Form XObjects. So, the OCG still exists on the newly spawned page, but is now disconnected. In fact it's a new OCG with the same name.  I think this is a bit of a bug. The spawn fuction should better manage the OCGs. It should either not make a new copy of the OCG dicitonary, or it should add the new copy to the OC Properties at the top level so Acrobat knows about it. Leaving dangling OCGs around seems like a bad practice. 

 

TOPICS
PDF forms

Views

62

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

Javascript to toggle layer visibility doesn't work on pages created from page templates

Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

I have a RPG character sheet with a toggle switch for a layer visibility. It works great on the main document pages but when I add new pages from templates the script doesn't work on the new pages.
Im using this script to toggle the visibility:

var layers = this.getOCGs();
for(var i=0;i<this.layers.length;i++) {
    if(layers[i].name == "Extras") {
        layers[i].state = !(layers[i].state);
    }
} 

 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

I took a quick look at this. Page spawing works by copying all the page contents for the template page into a single XObject. Which makes sense. But it makes new copies of all the indirect objects, and most OCGs are added as Form XObjects. So, the OCG still exists on the newly spawned page, but is now disconnected. In fact it's a new OCG with the same name.  I think this is a bit of a bug. The spawn fuction should better manage the OCGs. It should either not make a new copy of the OCG dicitonary, or it should add the new copy to the OC Properties at the top level so Acrobat knows about it. Leaving dangling OCGs around seems like a bad practice. 

 

TOPICS
PDF forms

Views

63

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
Oct 14, 2020 0
Most Valuable Participant ,
Oct 14, 2020

Copy link to clipboard

Copied

Do those pages have layers, too?

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...
Oct 14, 2020 0
Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

Yes. They are made in the same document on Illustrator. The extras layer is named the same on all pages.

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...
Oct 14, 2020 0
Most Valuable Participant ,
Oct 14, 2020

Copy link to clipboard

Copied

The question is whether the layer gets included in the Template object and then spawned to the new pages... I haven't tested that before. It's an interesting question. Can you share a sample 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...
Oct 14, 2020 0
Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

Sadly I cannot share it because it's not yet officially released. I'll do a mockup with same funtionality later.


The funny thing is when I go to Organize Pages and manually restore the templates to document, the layers work normally.

 

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...
Oct 14, 2020 0
Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

I have mockup version behind MEGA-link: Mockup test 

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...
Oct 14, 2020 0
Adobe Community Professional ,
Oct 14, 2020

Copy link to clipboard

Copied

Templates and layers are incompatible: templates ignore layers.

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...
Oct 14, 2020 1
Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

Well, that's a shame. 😞
I was hoping to make some cool character sheet appearance switches but with this knowledge I'll just do different versions of the same sheet.

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...
Oct 14, 2020 0
Community Beginner ,
Oct 14, 2020

Copy link to clipboard

Copied

Can I first create a page from template and then add a layer with content to it or is it locked even after creation?

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...
Oct 14, 2020 0
Most Valuable Participant ,
Oct 14, 2020

Copy link to clipboard

Copied

That should be possible, yes.

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...
Oct 14, 2020 1
Adobe Community Professional ,
Oct 15, 2020

Copy link to clipboard

Copied

You can add layers in a template page, but it will spawn a "flat" page with all layers merged.

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...
Oct 15, 2020 0
Adobe Community Professional ,
Oct 15, 2020

Copy link to clipboard

Copied

I took a quick look at this. Page spawing works by copying all the page contents for the template page into a single XObject. Which makes sense. But it makes new copies of all the indirect objects, and most OCGs are added as Form XObjects. So, the OCG still exists on the newly spawned page, but is now disconnected. In fact it's a new OCG with the same name.  I think this is a bit of a bug. The spawn fuction should better manage the OCGs. It should either not make a new copy of the OCG dicitonary, or it should add the new copy to the OC Properties at the top level so Acrobat knows about it. Leaving dangling OCGs around seems like a bad practice. 

 

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...
Oct 15, 2020 1