Highlighted

Export layer with image as PNG in InDesign

New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Try by making the following change.

 

pages[k].allPageItems //Change this to the following
pages[k].pageItems

 

The problem is suspect is that your code selects the pageItem as well the png inside it along with other pageItems. However, only the selection of the parent pageItem sans the png would be needed to create a successful group.

Another thing to note is that this code will fail if the layer would have objects placed on different spreads, as we can only select objects of a single spread. So you may want to handle this condition as well.

-Manan

TOPICS
How to, Import and export, Scripting

Views

63

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Export layer with image as PNG in InDesign

New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

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.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Try by making the following change.

 

pages[k].allPageItems //Change this to the following
pages[k].pageItems

 

The problem is suspect is that your code selects the pageItem as well the png inside it along with other pageItems. However, only the selection of the parent pageItem sans the png would be needed to create a successful group.

Another thing to note is that this code will fail if the layer would have objects placed on different spreads, as we can only select objects of a single spread. So you may want to handle this condition as well.

-Manan

TOPICS
How to, Import and export, Scripting

Views

64

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 15, 2020 0
Adobe Community Professional ,
Sep 15, 2020

Copy link to clipboard

Copied

Try by making the following change.

 

pages[k].allPageItems //Change this to the following
pages[k].pageItems

 

The problem is suspect is that your code selects the pageItem as well the png inside it along with other pageItems. However, only the selection of the parent pageItem sans the png would be needed to create a successful group.

Another thing to note is that this code will fail if the layer would have objects placed on different spreads, as we can only select objects of a single spread. So you may want to handle this condition as well.

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0
Kai5E45 LATEST
New Here ,
Sep 28, 2020

Copy link to clipboard

Copied

Thank you so much. I have solved it slightly differend but anyway your explination was very helpfull.

 

Kind regards

Kai

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 0