Skip to main content
Claire_Chr
Participating Frequently
March 10, 2023
Answered

Script : Layers in pdf

  • March 10, 2023
  • 2 replies
  • 933 views

Hello to all,

In order to simplify the life of my colleagues, I would like to make a script that exports all the layers of my document in separate pdfs with the name of the layer in the title of my pdfs.

 

I'm already using the script below which I've cobbled together as best I could (it's not my job, I do what I can). But I don't see how to add the record of each layer automatically.

 

d = app.activeDocument;

// PDF preset
preset1 = app.pdfExportPresets.itemByName("Print");
preset2 = app.pdfExportPresets.itemByName("Web");

// Date
var today = new Date();
var month = (today.getMonth() + 1) < 10 ? "0"+(today.getMonth() + 1) : (today.getMonth() + 1);
var dateString = (today.getFullYear()+"").substr(2, 2) + "-" + month + "-" + today.getDate();

if (!(preset1.isValid && preset2.isValid)){
alert("Au secours Claire! Vérifier les joboptions.");
exit();
}
if (d.saved){
thePath = String(d.fullName).replace(/\..+$/, "") + ".pdf";
thePath = String(new File(thePath).saveDlg());
}
else{
thePath = String((new File).saveDlg());
}
thePath = thePath.replace(/\.pdf$/, "");
name1 = thePath+" Classic - "+dateString+".pdf";
name2 = thePath+" Héritage - "+dateString+".pdf";
name3 = thePath+" Classic - PRINT "+dateString+".pdf";
name4 = thePath+" Héritage - PRINT "+dateString+".pdf";

app.pdfExportPreferences.pageRange = "1,2";
d.exportFile(ExportFormat.PDF_TYPE, new File(name1), false, preset2);
app.pdfExportPreferences.pageRange = "3,4";
d.exportFile(ExportFormat.PDF_TYPE, new File(name2), false, preset2);
app.pdfExportPreferences.pageRange = "1,2";
d.exportFile(ExportFormat.PDF_TYPE, new File(name3), false, preset1);
app.pdfExportPreferences.pageRange = "3,4";
d.exportFile(ExportFormat.PDF_TYPE, new File(name4), false, preset1);

 

If any of you have a solution you would make my life and that of my colleagues much easier.

Thank you very much for your help.
Thanks a lot.

Claire.

This topic has been closed for replies.
Correct answer Peter Kahrel

Oops, indeed! The script should enable only one layer for each export. So first disable all layers, then enable only the one you're exporting.

layers = d.layers.everyItem().getElements();
// Disable all layers
layers.everyItem().visible = false;
for (i = 0; i < layers.length; i++) {
  layers[i].visible = true;  // Enable a layer
  app.pdfExportPreferences.pageRange = "1,2";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name1.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset2
  );
  app.pdfExportPreferences.pageRange = "3,4";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name2.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset2
  );
  app.pdfExportPreferences.pageRange = "1,2";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name3.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset1
  );
  app.pdfExportPreferences.pageRange = "3,4";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name4.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset1
  );
  layers[i].visible = false; // And disable it again
}

2 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
March 13, 2023

Oops, indeed! The script should enable only one layer for each export. So first disable all layers, then enable only the one you're exporting.

layers = d.layers.everyItem().getElements();
// Disable all layers
layers.everyItem().visible = false;
for (i = 0; i < layers.length; i++) {
  layers[i].visible = true;  // Enable a layer
  app.pdfExportPreferences.pageRange = "1,2";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name1.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset2
  );
  app.pdfExportPreferences.pageRange = "3,4";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name2.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset2
  );
  app.pdfExportPreferences.pageRange = "1,2";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name3.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset1
  );
  app.pdfExportPreferences.pageRange = "3,4";
  d.exportFile(
    ExportFormat.PDF_TYPE,
    new File(name4.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset1
  );
  layers[i].visible = false; // And disable it again
}
Claire_Chr
Participating Frequently
March 13, 2023

Thanks a lot fgor your help ! It's perfect! 🙂

Peter Kahrel
Community Expert
Community Expert
March 10, 2023

You need to cycle through the layers and add their names to the output PDF names. Something like the following. It replaces your export block:

 

layers = d.layers.everyItem().getElements();
for (i = 0; i < layers.length; i++) {
  app.pdfExportPreferences.pageRange = "1,2";
  // insert the layer name before the pdf extension
  d.exportFile(
    ExportFormat.PDF_TYPE, 
    new File(name1.replace(/(?=.pdf$)/, '-'+layers[i].name)), false, preset2
  );
  // The same fo the three remaining exports
}

 

 

P.

Claire_Chr
Participating Frequently
March 13, 2023

This is fantastic! It works perfectly.
Thank you very much for your help, it will make my life easier every day!