InDesign script to resize images in XML

You can fill an InDesign document with XML content. The XML may contain references to images. These are automatically retrieved when loading the XML. If these images are larger than the page width, the image is not visible and the text that comes after it falls away. The script resolves this issue.

You can fill an inDesign document with XML content. The XML can contain references to images. These are retrieved automatically when loading the XML. If these images are larger than the page width, the image cannot be seen and the text that comes after that is dropped. The script resolves this issue.

XML for inDesign

The XML export from Publishtool often contains references to images. You can download the corresponding images from Publishtool in a folder. The moment the XML is imported into inDesign, the images are automatically placed in the running text. Sometimes the images are too large and don't fit on the page. In that case, the images are not visible and the running text after the image has also disappeared. In the XML, a reference to an image can occur inline (in a paragraph) or outline (outside a paragraph). The XML looks like this:

<figure aid:pstyle="figure"><img href="file:///images/color-navigation.png" alt="Color navigation" width="310"/> 
<figcaption aid:pstyle="figcaption">Color navigation in a coordinate system </figcaption></figure> 

<p aid:pstyle="p"><fig type="inline" aid:cstyle="fig"><img href="file:///images/dna-plastiek-koe.png" width="310"/> 
<figcaption aid:pstyle="figcaption">Match DNA profile with a cow</figcaption></fig><fig type="inline" aid:pstyle="fig"><img href="file:///images/dna-mondriaan-bloeiende-boom.png" width="310"/> 
<figcaption aid:pstyle="figcaption">Match DNA profile with a painting</figcaption></fig></p>

<p aid:pstyle="p"><img-left aid:cstyle="img-left" type="inline"><img href="file:///images/avatar.png" width="100" height="100"/></img-left>Text</p>

<math aid:pstyle="math"><Table aid:table="table" aid:trows="1" aid:tcols="2" aid5:tablestyle="equation"><Cell aid:table="cell" aid5:cellstyle="eq" aid:crows="1" aid:ccols="1" aid:ccolwidth="460.00"><eq><img href="file:///math/tex-57w2bC.pdf" width="144%"/></eq></Cell><Cell aid:table="cell" aid5:cellstyle="eq_nr" aid:crows="1" aid:ccols="1" aid:ccolwidth="60.00"><p_eq_nr aid:pstyle="p_eq_nr"/></Cell></Table></math>
 
<p aid:pstyle="p">Dit symbool is <symbol aid:cstyle="symbol"><img href="file:///symbol/tex-55pkL7.pdf" width="144%"/></symbol> lambda</p>

Script to adjust image width to page width

I wrote a script for inDesign that attaches an object style to the images and reduces the size of the images if the image is too large for the text container. The image is also placed in a text frame together with a caption. This makes it possible to ensure that all images together with the caption are given a fixed place in the document in relation to the anchor. You could also number the captions, so that for each caption, for example, there will be "Fig. #" in which # is replaced by the automatic numbering.

Brief explanation of how script works

This script is an extension of the FitAnchorToColumn v8 script posted by vinny38 on the Adobe forum. First, an object style 'img' is created with the option "Fill frame proportionally" so that if the size of the image is adjusted, the frame scales along. This is the default object style for the images. After that, the script creates object styles based on the imported XML. For this XML, these are the object styles 'figure', 'fig', 'img-left', 'math' and 'symbol'. Then an inline text frame is placed around the image for when the image has a caption. These newly created object styles are given the option "Text wrap" on "Wrap around object shape" and "Text frame Auto Size Options" on "Height and Width".

After that, the script creates object styles based on the imported XML. For this XMl, these are the object styles 'figure', 'fig', 'img-left', 'math' and 'symbol'. Then an inline textframe is placed around the image for when the image has a caption. These newly created object styles are given the option "Text wrap" on "Wrap around object shape" and "Text frame Auto Size Options" on "Height and Width".

After that, the images are scaled:

  • If the width is less than the column width, the image gets its original width
  • If an attribute has width in the XML, the size is determined from this width
  • If the width exceeds the column width, the image is scaled.

Because each type of image gets a separate object style and is also in a specific paragraph style, indesign the placement and sizes can be arranged per type of image. The captions can therefore easily be numbered automatically.

If the XML is updated with new content, you can reload the XML. The adjustments you have made in indesign will remain intact. Only when new images have been added do you have to run the script to scale the new images and to assign the right object styles.

You can download the script on Github.