Export layer with image as PNG in InDesign
Hey community,
I am desperate.I have a Script for Adobe InDesign 2020, which performes an Export as PNG for each layer. It is important to me, that only the objects of the specific layer are within the Export and that there are no borders or whatsever are around it.
I am successfull with the following Script:
/// <summary>
/// Exportiert die Inhalte eines Layers mit transparenten Hintergrund als PNG
/// </summary>
function exportLayer(layer, filename) {
var activeDocument = app.activeDocument;
var pages = activeDocument.pages;
var layerItems = [];
for (var k = 0; k < pages.length; k++) {
var items = pages[k].allPageItems;
for (var i = 0; i< items.length; i++) {
var item = items[i];
if (item.itemLayer.name == layer) {
layerItems.push(item);
}
}
}
app.selection = layerItems;
var path = settingDestinationFolder + filename;
if(app.selection.length > 1){
try {
var myObj = app.activeWindow.activePage.groups.add(app.selection);
myObj.exportFile(ExportFormat.PNG_FORMAT, path, false);
myObj.ungroup();
} catch (e) {
alert(e);
}
}else{
app.selection[0].exportFile(ExportFormat.PNG_FORMAT, path, false);
}
}Everything works fine, as long as I do not have an Image within the layer. But if I place an Image (also a PNG) on a layer before export, the Script above fails by Grouping the selection. InDesign tells me: "invalid parameter".
When I remove the PNG from the layer, it works well again.
If I do the exact same thing within the UI, everything works just fine.
My PNG Export options are like that:
function initPngExportOptions(dpi, transparentBackground) {
writeLog ("INFO", "Initiieren der Export-Optionen");
if (typeof dpi === 'string' || dpi instanceof String) { dpi = parseFloat(dpi); }
app.pngExportPreferences.transparentBackground = transparentBackground;
app.pngExportPreferences.exportResolution = dpi;
app.pngExportPreferences.pngQuality = PNGQualityEnum.MAXIMUM;
app.pngExportPreferences.pngExportRange = PNGExportRangeEnum.EXPORT_ALL;
app.pngExportPreferences.useDocumentBleeds = false;
}Would be very thankfull for help.
