Export layer with image as PNG in InDesign

New Here ,
Sep 15, 2020 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.

TOPICS
How to , Import and export , Scripting

Views

656

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Sep 15, 2020 Sep 15, 2020
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. S...

Likes

Translate

Translate
Adobe Community Professional ,
Sep 15, 2020 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
community guidelines
New Here ,
Sep 28, 2020 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
community guidelines
Community Beginner ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

Hi Kai and Manan, 

 

I would also be very much helped by such a script. Could you kindly put the last version together and zip it? It will be very much appreciated.

 

Thanks in advance!

Cristina

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
community guidelines
Adobe Community Professional ,
Nov 03, 2020 Nov 03, 2020

Copy link to clipboard

Copied

Hi @allthingsmoving,

You can use the following script, I consolidated all the snippets into one. Hopefully, it will work

 

function initPngExportOptions(dpi, transparentBackground) {
    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;
}

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].pageItems;
        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);
    }
}

var settingDestinationFolder = "path to your export file" //Change it to the path of the folder where you want to export, end it with a /
initPngExportOptions(300, true)  //Change the values as needed
exportLayer("layername", "myfile.png") //Change the values as needed

 

Make changes in the last three lines for the values that you need and then run it. In order to execute this just copy the code paste it into a text editor like notepad and save the file with jsx format. To install/execute the script see the following article

https://helpx.adobe.com/illustrator/using/automation-scripts.html

-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
community guidelines
New Here ,
May 26, 2022 May 26, 2022

Copy link to clipboard

Copied

LATEST

Very good script! It will help me a lot. Would it be possible to export groups of objects grouped with separate images? sometimes I have 3 groups of objects on the page that need to be exported separately. thank you so much!

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
community guidelines