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

PDF Merge Layer - Join Layer with same name

New Here ,
Jul 15, 2024 Jul 15, 2024

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.");

 

TOPICS
Edit and convert PDFs , JavaScript , PDF
1.6K
Translate
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jul 16, 2024 Jul 16, 2024

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. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
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 ,
Jul 18, 2024 Jul 18, 2024

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/

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
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 ,
Jul 15, 2024 Jul 15, 2024

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();

Translate
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
New Here ,
Jul 16, 2024 Jul 16, 2024

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!

Screenshot 2024-07-16 115926.pngexpand image

Translate
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 ,
Jul 16, 2024 Jul 16, 2024

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. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
New Here ,
Jul 18, 2024 Jul 18, 2024

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?

Translate
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 ,
Jul 18, 2024 Jul 18, 2024

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/

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
New Here ,
Jul 18, 2024 Jul 18, 2024
LATEST

Thank you very much for your help. I will take a look on your recommentations!

Translate
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