Skip to main content
adamg86737138
Inspiring
October 30, 2015
Answered

Data merge shrink images down proportionally to fit in frame if it doesn't fit at 100%

  • October 30, 2015
  • 3 replies
  • 863 views

I'm doing a large data merge of some labels and would like the images to be shrunk proportionally to fit the frame if it is too large to fit at 100%.

From what I have read this is not possible purely with the data merge function in InDesign. However, I figure it is possible using a script? Also, there are three frames and I would like this to be applied to only one of the frames (again, if this is possible).

If this is all doable, how would I go about it? Is anyone able to write a script for me? Or is there one existing that I could use? I have had a look but couldn't find anything similar.

Thanks in advance!

Adam

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

If you call pageItem by its name - it returns 1st occurence not all of them.

You need to iterate through all pageItems and check names.

Alike this:

var

  mPI = app.activeDocument.allPageItems,

  cPI, cGB, cW, cH;

while ( cPI = mPI.pop() ) {

  if (cPI.name == "3D") {

       cPI.fit(FitOptions.PROPORTIONALLY);

       continue;

       }

  if (cPI.name == "PROFILE") {

       cGB = cPI.graphics[0].geometricBounds;

       cW = cGB[3] - cGB[1];

       cH= cGB[2] - cGB[0];

       if (cW > 40 || cH > 43)

            cPI.fit(FitOptions.PROPORTIONALLY);

       }

  }

If cPI has no graphics ==> will return error

(so you may need to check it if possible to happen)

Jarek

3 replies

adamg86737138
Inspiring
November 5, 2015

I've had a go at writing my own script. It did work, however only on the frames by those item names on the first page in the data merged document.

I've since then tried to put it in arrays as I believed that would be how to apply the effects to all frames by that name. It doesn't work however and errors that .length is not supported...

//SHRINKS THE PROFILE IMAGE PROPORTIONALLY IF THE WIDTH IS GREATE THAN 42mm

var ProfileFrame = app.activeDocument.pageItems.Item("PROFILE");

var ProfileGraphic = app.activeDocument.pageItems.Item("PROFILE").graphics[0];

var gBounds = ProfileGraphic.geometricBounds;

var y1 = gBounds[0];

var x1 = gBounds[1];

var y2 = gBounds[2];

var x2 = gBounds[3];

//DO CALCULATIONS

var frameWid = x2 - x1;

var frameHgt = y2 - y1;

for (i=0; i<ProfileFrame.length; i++){

  if (frameWid > 40 || frameHgt > 43) {

    ProfileFrame.fit(FitOptions.PROPORTIONALLY);

  }

}

//SHRINKS THE 3D IMAGE PROPORTIONALLY

var RFrame = app.activeDocument.pageItems.Item("3D");

for (i=0;i<RFrame.length;i++){

  RFrame.fit(FitOptions.PROPORTIONALLY);

}


What am I doing wrong here? Hope you can tell what's going on.

Thanks!

Jump_Over
Jump_OverCorrect answer
Legend
November 5, 2015

Hi,

If you call pageItem by its name - it returns 1st occurence not all of them.

You need to iterate through all pageItems and check names.

Alike this:

var

  mPI = app.activeDocument.allPageItems,

  cPI, cGB, cW, cH;

while ( cPI = mPI.pop() ) {

  if (cPI.name == "3D") {

       cPI.fit(FitOptions.PROPORTIONALLY);

       continue;

       }

  if (cPI.name == "PROFILE") {

       cGB = cPI.graphics[0].geometricBounds;

       cW = cGB[3] - cGB[1];

       cH= cGB[2] - cGB[0];

       if (cW > 40 || cH > 43)

            cPI.fit(FitOptions.PROPORTIONALLY);

       }

  }

If cPI has no graphics ==> will return error

(so you may need to check it if possible to happen)

Jarek

adamg86737138
Inspiring
November 5, 2015

Hi Jarek,

Thanks a lot for writing this for me, it works like a charm! Exactly what I needed.

Adam

Colin Flashman
Community Expert
Community Expert
October 31, 2015

Not sure why you would need a script when you could just change the image fitting options of the pictures being imported into indesign. This is done from the data merge flyout and it's called "content placement options" and choose the one that says "fit image to frame".

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
adamg86737138
Inspiring
November 3, 2015

Hi Colin,

I'm aware of this flyout in InDesign, however I believe that this applies to all image frames in the document? I'm after some way to only affect one frame of the three frames in the document.

In the example below, the <<PROFILE>> image frame is the one I would like to display images at 100% if smaller than the frame, otherwise shrink it down so it's not cropped off the edges.

The other two frames: <<3D>> and <<BARCODE>> will have "fit images proportionally".

Jump_Over
Legend
October 30, 2015

Hi,

No magic.

Script is a similar InDesign user as any human being.

You need to explain to it

     - what to do

     - how to find a target object

     - what to do if job cant be done.

However:

Consider that many "1-step" goals can be reached by UI tools and utilities, alike

templates, predefined objectStyles and so on...

Jarek