Skip to main content
barnabyr36467505
Participating Frequently
January 31, 2020
Answered

Javascript to turn off ocg / layers beginning with a text string

  • January 31, 2020
  • 3 replies
  • 2895 views

Hi, I'm trying to turn off all layers that begin with a text string.

So far I have:

var l = this.getField("SELECT.Language").valueAsString;

var n = this.getField("SELECT.Number").valueAsString;

for (var x=0; x < ResetOCGs.length; x++)

{ ResetOCGs[x].state = false; }

for (var x=0; x < docOCGs.length; x++)

{

if(docOCGs[x].name == "Logo" ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l + ".1" ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l + ".2" ) { docOCGs[x].state = false; }

if(docOCGs[x].name == l + ".3" ) { docOCGs[x].state = false; }

if(docOCGs[x].name == l + ".4" ) { docOCGs[x].state = false; }

}

I'd ideally like to get rid of Reset OCGs a turn off layers that begin with l + ".3".

I was thinking something like l + ".3*", with the asterisk acting as a wildcard but it doesn't seem to work.

Can anyone help please?

This is a form created in Acrobat, with all fields created in Acrobat.

This topic has been closed for replies.
Correct answer Thom Parker

Hmm sorry Thom, I'm beginning to think I'm expecting too much of acrobat & asking too much of your time that you've already given me. The language selector works but it doesn't turn off every OCG. So if I have got halfway through filling out English for example, then realise that I should've been filling out French, the language selector needs to basically reset the form. I have done this with a form reset to reset the fields but it doesn't turn off any of the english OCGs that might have been turned on. Is there some simple way of turning off all the OCGs, then running the code you've provided to turn the correct language OCGs on?


Same as you did before, in the first post.

Put this at the top:

 

for (var x=0; x < docOCGs.length; x++)

{ docOCGs[x].state = false; }

 

You are a good ways along in your process, don't loose hope. This is complex, and you're having exactly the same difficulties every programmer has when they are starting out. Your thinking in a straight line about how you want it to work, but you also have to think about how you don't want it to work. You're strategy has to take "all" scenerios into account. Cover all the possibilities. Think about this first, before committing to a specific methodology. 

 

Also, when you asking for help, remember that we don't know anything about your setup and goals. So unless it's real simple, we're not going to understand the total complexity and are not going to be able to help with figuring out all the possible scenerios. We can only help with the mechanics of a specific issue, unless of course you hire me 😉

 

 

 

3 replies

barnabyr36467505
Participating Frequently
February 3, 2020

ok, so thanks to a combination of advice from try67 & Thom_Parker, I have made some progress.

The form is for different languages (var l) & for a different number of groups (var n).

The following works for group 1 in that it doesn't use the resetOCGs var & is quicker but it's not quite there:

 

var l = this.getField("SELECT.Language").valueAsString;

var n = this.getField("SELECT.Number").value;

for (var x=0; x < docOCGs.length; x++)

{ if(docOCGs[x].name.indexOf(l + ".") == 0 ){ docOCGs[x].state = false; }

if(docOCGs[x].name == l + "." + n) { docOCGs[x].state = true; } }

 

This shows OCGs that begin with language.number.

However I need it to show the levels before.

So if the group is 1, it shows OCGs that are language.1

But if the group is 2, it needs to show language.1 & language.2

For group 3, it shows language.1, language.2 & language.3 & so on.

I have tried:

 

for (var x=0; x < docOCGs.length; x++)

{ if(docOCGs[x].name.indexOf(l + ".") == 0 ) { docOCGs[x].state = false; }

if(docOCGs[x].name == l + "." + <n) { docOCGs[x].state = true; }

if(docOCGs[x].name == l + "." + n) { docOCGs[x].state = true; }

if(docOCGs[x].name == l + "." + >n) { docOCGs[x].state = false; }}

 

But the greater or less than indicator throws up a syntax error, I can't seem to find anything on if value is less than or greater than a var.

It feels so close, can you help please?

Thom Parker
Community Expert
Community Expert
February 3, 2020

You haven't actually explained your naming convention for the layers and explicitly layed out the rules for the languange and group selections.

It also seems like there are two completely different things going on, i.e. language and group. 

 

If you can give a clear and short explanation I can provide a method for diplaying the correct layers.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
barnabyr36467505
Participating Frequently
February 3, 2020

Ah sorry Thom, I thought it was going to be a simple tweak, here goes:

So we have different languages using a venue, with a number of different groups in the venue each night.

The form is to provide a timetable showing what groups meet & when, & this is to be provided in each language.

So layers are named as just "language" or "language.number" or "language.number.other", e.g.

English or Spanish.2 or Polish.4.time

The language radio selector is the first selection made in the form, so I need the language radio selector to turn off all layers, other than those that have the selected language in the name of the layer.

Once the language is selected another radio selector selects how many groups are using the venue that will be shown on the page, this is currently 4 possible groups but could expand up to 10. I need this selector to turn off the layers with numbers that are greater than the radio selection & to turn on layers with numbers that are equal to or less than the radio selection.

For the language selector I currently have:

var ResetOCGs = this.getOCGs();

var docOCGs = this.getOCGs();

var l = this.getField("SELECT.Language").valueAsString;

for (var x=0; x < ResetOCGs.length; x++)

{ ResetOCGs[x].state = false; }

for (var x=0; x < docOCGs.length; x++)

{ if(docOCGs[x].name == "Logo" ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l ) { docOCGs[x].state = true; } }

 

I'd like to get rid ResetOCGs as it is very slow & have something like (the bits I'm struggling with are in red):

var docOCGs = this.getOCGs();

var l = this.getField("SELECT.Language").valueAsString;

for (var x=0; x < docOCGs.length; x++)

if(docOCGs[x].name == "all layers" ) { docOCGs[x].state = false; } (I tried Try67's solution for this but couldn't select all layers) This could be all layers other than one layer called just "Logo"

if(docOCGs[x].name == "Logo" ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l ) { docOCGs[x].state = true; } }

 

Then for the number of groups I would need something like:

var n = this.getField("SELECT.Number").value;

for (var x=0; x < docOCGs.length; x++)

{ if(docOCGs[x].name.indexOf(l + ".") == 0 ) { docOCGs[x].state = false; }

if(docOCGs[x].name.indexOf(l + "." + (<=n)) == 0 ) { docOCGs[x].state = true; }

if(docOCGs[x].name.indexOf(l + "." + (>n)) == 0 ) { docOCGs[x].state = false; } }

So if the group number selector was 1 only layers that were language.1 would be on.

For 2, layers with language.1 & language.2 would be on

For 3, layers with language.1 & language.2 & language.3 would be on.

 

I hope that makes more sense, the form has kind of expanded over time & is getting unwieldy.

Please let me know if I haven't been clear enough, I really do appreciate your help.

 

Thom Parker
Community Expert
Community Expert
January 31, 2020

Where do "docOCG" and "resetOCG" come from ? 

 

If you want to do pattern matching you need to use a Regular Expression.

https://acrobatusers.com/tutorials/text-matching-regular-expressions/

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
barnabyr36467505
Participating Frequently
February 3, 2020

Thanks for your reply Thom, I have earlier in the workflow of the form:

var ResetOCGs = this.getOCGs();

var docOCGs = this.getOCGs();

var l = this.getField("SELECT.Language").valueAsString;

for (var x=0; x < ResetOCGs.length; x++)

{

ResetOCGs[x].state = false;

}

for (var x=0; x < docOCGs.length; x++)

{

if(docOCGs[x].name == "Logo" ) { docOCGs[x].state = true; }

if(docOCGs[x].name == l ) { docOCGs[x].state = true; }

}

The reset is to turn all OCGs off but it takes a few seconds for the reset to run for some reason, so I'd like to not use it more than I have to.

If I can use some pattern matching to turn some of the OCGs off rather than using the reset, it will make the form quicker.

Thanks for the link, I will have a look.

try67
Community Expert
Community Expert
January 31, 2020

You can use something like this:

 

if (docOCGs[x].name.indexOf(l)==0) { docOCGs[x].state = true; }

 

This will match all the OCGs whose name begin with the value of that variable.

barnabyr36467505
Participating Frequently
February 3, 2020

Thanks try67 for your reply, I will give it a go & reply back.