Copy link to clipboard
Copied
Hi!
I merged single page PDFs, all with the same Layer structure. After merged the document has for every page its own Layergoup and inside the layers. I like to merge the Layer with the same name to a single layer, to switch on/off the elements over the whole document with one click instead to switch every layer on every page. Here is a script which nearly does what i what. It merge the layers but when i switch visibility form a layer it only changes on page 1. It seems the the element on the other pages get "layerless". I cant hide them any more. Please can anybody help me. Here is the script and a demo file:
var ocgs = this.getOCGs();
var layers = {};
var newOrder = [];
// Funktion, um alle Ebenen sichtbar zu machen
function makeAllLayersVisible() {
for (var i = 0; i < ocgs.length; i++) {
var ocg = ocgs[i];
ocg.state = true; // Setzt jede Ebene auf sichtbar
}
}
// Funktion, um Ebenen zu kombinieren
function combineLayers() {
for (var i = 0; i < ocgs.length; i++) {
var ocg = ocgs[i];
var name = ocg.name;
if (!layers[name]) {
layers[name] = ocg;
newOrder.push(ocg);
} else {
// Kombinieren von Zuständen und Kindern
var existingOCG = layers[name];
if (ocg.state !== undefined) {
existingOCG.state = ocg.state || existingOCG.state;
}
if (ocg.children) {
if (!existingOCG.children) {
existingOCG.children = [];
}
for (var j = 0; j < ocg.children.length; j++) {
existingOCG.children.push(ocg.children[j]);
}
}
}
}
// Setze alle Ebenen auf sichtbar, falls sie nicht schon sichtbar sind
for (var key in layers) {
if (layers.hasOwnProperty(key)) {
layers[key].state = true;
}
}
}
// Alle Ebenen zuerst sichtbar machen
makeAllLayersVisible();
// Ebenen kombinieren
combineLayers();
// Neue Reihenfolge der Ebenen setzen
this.setOCGOrder(newOrder);
console.println("Layers combined successfully and set to visible.");
Copy link to clipboard
Copied
So first, OCGs cannot be combined with JavaScript. They can only be rearranged.
If you want the layers with the same name to be merged, you'll have to do it manually or write a plug-in to do it.
Or, put buttons on the PDF that turn the layers on and off the way you want.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The UXP toolset does not apply to Acrobat. Here's a link to the Acrobat plug-in SDK.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/plugin/index.html
In order to use the SDK you will need a good understanding of the internal PDF structure.
This tool will help a lot.
https://www.windjack.com/product/pdfcanopener/
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
function toggleLayers()
{
if(this.getOCGs()[0].state==false)
{
var ocgs = this.getOCGs();
var layers = {};
var newOrder = [];
// Funktion, um alle Ebenen sichtbar zu machen
function makeAllLayersVisible() {
for (var i = 0; i < ocgs.length; i++) {
var ocg = ocgs[i];
ocg.state = true; // Setzt jede Ebene auf sichtbar
}
}
// Funktion, um Ebenen zu kombinieren
function combineLayers() {
for (var i = 0; i < ocgs.length; i++) {
var ocg = ocgs[i];
var name = ocg.name;
if (!layers[name]) {
layers[name] = ocg;
newOrder.push(ocg);
} else {
// Kombinieren von Zuständen und Kindern
var existingOCG = layers[name];
if (ocg.state !== undefined) {
existingOCG.state = ocg.state || existingOCG.state;
}
if (ocg.children) {
if (!existingOCG.children) {
existingOCG.children = [];
}
for (var j = 0; j < ocg.children.length; j++) {
existingOCG.children.push(ocg.children[j]);
}
}
}
}
// Setze alle Ebenen auf sichtbar, falls sie nicht schon sichtbar sind
for (var key in layers) {
if (layers.hasOwnProperty(key)) {
layers[key].state = true;
}
}
}
// Alle Ebenen zuerst sichtbar machen
makeAllLayersVisible();
// Ebenen kombinieren
combineLayers();
// Neue Reihenfolge der Ebenen setzen
this.setOCGOrder(newOrder);
}
else
{
var ocgs=this.getOCGs();
for(var i=0;i<ocgs.length;i++)
{
this.ocgs[i].state=false;
}
}
}
//Run toggle function
toggleLayers();
Copy link to clipboard
Copied
Hello,
thanks for your quick reply.
Unfortunately, it still does not behave as desired. I have attached a simplified test PDF where you can easily see if it works. There should only be one layer of text and one layer of images in which the respective elements are stored.
The screenshot shows the current state.
Perhaps you could help me again.
Thank you very much!
Copy link to clipboard
Copied
So first, OCGs cannot be combined with JavaScript. They can only be rearranged.
If you want the layers with the same name to be merged, you'll have to do it manually or write a plug-in to do it.
Or, put buttons on the PDF that turn the layers on and off the way you want.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Oh, what a bity.
Okay, i already started to write plugins for indesign but in the UXP developer Tools I´m only able to start projects for Photoshop, Indesign and XD. You can I write a plugin for Acrobat?
Copy link to clipboard
Copied
The UXP toolset does not apply to Acrobat. Here's a link to the Acrobat plug-in SDK.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/plugin/index.html
In order to use the SDK you will need a good understanding of the internal PDF structure.
This tool will help a lot.
https://www.windjack.com/product/pdfcanopener/
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you very much for your help. I will take a look on your recommentations!

