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

Script : Layers in pdf

Explorer ,
Mar 10, 2023 Mar 10, 2023

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.

TOPICS
Import and export , Scripting
720
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

correct answers 1 Correct answer

Community Expert , Mar 13, 2023 Mar 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
  )
...
Translate
Community Expert ,
Mar 10, 2023 Mar 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.

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
Explorer ,
Mar 13, 2023 Mar 13, 2023

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

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
Explorer ,
Mar 13, 2023 Mar 13, 2023

Oops, I got a little carried away

So I wrote the following script thanks to your advice

 

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

layers = d.layers.everyItem().getElements();
for (i = 0; i < layers.length; i++) {
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
);
}

 

The exports have the right presets and names but the layer that appears is always the same.

 

I have a UK, FR, DE layer, and if I run my script with just the UK layer open all my pdf's will have the contents of the UK layer but with the name FR or DE.

How do I correct the script so that on the DE pdf I have the contents of the DE layer please?

Thanks again for your help.

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 ,
Mar 13, 2023 Mar 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
}
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
Explorer ,
Mar 13, 2023 Mar 13, 2023
LATEST

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

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