Skip to main content
Participant
July 25, 2025
Question

Java script to show/hide layers and export as DFX and PNG

  • July 25, 2025
  • 1 reply
  • 130 views

Hi Everyone.

I am working on a project that requires me to export 4 layers from an illustrator file,  1 into DXF format and 3 into PNG format. I can do this using actions (not hiding and showing each layer) but I'd prefer to be able to do it using a script as it is something that is required with each file and there are numerous files to do.

The 4 layers are called A, B, C and D. Layer A needs to be saved as a .DXF file to a certain specification and the file name to be "filenameA.dxf". Layers B,C and D need to be saved as .PNG files to certain specification and the file name to be "filenameB.png" etc. The files can be saved to a local drive in the same folder and then moved after. Thank you for taking time to read my post and I hope to hear from you soon.

1 reply

Charu Rajput
Community Expert
Community Expert
July 25, 2025

Hi,

You can try the following script

 

if (app.documents.length === 0) {
  alert("Please open a document before running this script.");
} else {
  var doc = app.activeDocument;
  var docPath = doc.fullName.parent;
  var docName = doc.name.replace(/\.[^\.]+$/, ""); // Remove extension

  // Export Layer A as DXF
  exportLayerAsDXF("A", docPath + "/" + docName + "A.dxf");

  // Export Layers B, C, and D as PNG
  exportLayerAsPNG("B", docPath + "/" + docName + "B.png");
  exportLayerAsPNG("C", docPath + "/" + docName + "C.png");
  exportLayerAsPNG("D", docPath + "/" + docName + "D.png");
  showAllLayers();
}

function exportLayerAsDXF(layerName, filePath) {
  try {
    var dxfLayer = app.activeDocument.layers.getByName(layerName);
  } catch (e) {
    alert("Layer not found: " + layerName);
    return;
  }

  hideAllLayers();
  dxfLayer.visible = true;
  var options = new ExportOptionsAutoCAD();
  options.exportFileFormat = AutoCADExportFileFormat.DXF;
  options.exportOption = AutoCADExportOption.MaximumEditability;
  options.version = AutoCADCompatibility.AutoCADRelease14;
  options.unit = AutoCADUnit.Millimeters;
  options.scaleLineweights = false;
  options.exportSelectedArtOnly = true;
  options.convertTextToOutlines = false;
  options.generateThumbnails = false;

  var dxfFile = new File(filePath);
  app.activeDocument.exportFile(dxfFile, ExportType.AUTOCAD, options);
}

function exportLayerAsPNG(layerName, filePath) {
  try {
    var pngLayer = app.activeDocument.layers.getByName(layerName);
  } catch (e) {
    alert("Layer not found: " + layerName);
    return;
  }

  hideAllLayers();
  pngLayer.visible = true;

  var exportOptions = new ExportOptionsPNG24();
  exportOptions.antiAliasing = true;
  exportOptions.transparency = true;
  exportOptions.artBoardClipping = true;
  exportOptions.horizontalScale = 100;
  exportOptions.verticalScale = 100;

  var pngFile = new File(filePath);
  app.activeDocument.exportFile(pngFile, ExportType.PNG24, exportOptions);
}

function hideAllLayers() {
  for (var i = 0; i < app.activeDocument.layers.length; i++) {
    app.activeDocument.layers[i].visible = false;
  }
}

function showAllLayers() {
  for (var i = 0; i < app.activeDocument.layers.length; i++) {
    app.activeDocument.layers[i].visible = true;
  }
}
Best regards
Participant
August 4, 2025

Hi

Thank you so much for this, unfortunately when I run the script it comes up with the following error;

"The operation cannot complete because of an unknown error. [2067]"

Kind regards