Skip to main content
Participating Frequently
January 22, 2021
Question

InDesign Server Scripts: templating with existing idml file

  • January 22, 2021
  • 5 replies
  • 659 views

I have a set of idml files I am using as templates to place text/images and convert to pdf.

Most templates are set up like this: 
A textframe
- title, body, inline textframes for photos
B textframe
- logo

I've sucessfully placed text/images. This is a sample js snippet of key parts. (If there is a better way to replace content please let me know.)

var user_name = "Sam";
var user_photo_1= "file location";
// Modify idml
// Clear old preferences
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
// Change text for "NAME"
app.findTextPreferences.findWhat = "NAME";
app.changeTextPreferences.changeTo =user_name;
app.changeText();
// Loop through stories then textframes
// Check contents of textframe to replace content
// Doing this for images since the above didn't work
switch (myTextFrames[j].contents) {
case "IMG_1":
    if (user_photo_1 == "") { myTextFrames[j].remove(); break; }
    myTextFrames[j].place(File(user_photo_1));
    myTextFrames[j].fit(FitOptions.CONTENT_TO_FRAME);
    myTextFrames[j].fit(FitOptions.FILL_PROPORTIONALLY);
    myTextFrames[j].fit(FitOptions.CENTER_CONTENT);
    break;
}

The issue I am having is when I have less content to fill the document space than expected. I need to:
1. trim textframe A.

2. move textframe B up.
3. trim document height.

Can someone point me in the right direction for getting this done? 
This topic has been closed for replies.

5 replies

Community Expert
February 2, 2021

Remove the parent of an image if you want to remove the graphic frame of the image plus the image itself.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
January 26, 2021

"Is it possible for the document to autosize, not just textframes?"

You mean the page's height and width? No. You have to calculate the page sizes and resize a given page.

In facing pages documents this can become very tricky, because pages will always glue to other pages horizontally. Or to the spine.

 

"Does the version of InDesign that our designers use to create templates dictate what version of InDesign Server we should use?"

I'd say: yes.

 

And another issue: Avoid by all means fonts that are made available through the Adobe Fonts service, because as far as I know you cannot work with Adobe Fonts on InDesign Server.

 

Regards,
Uwe Laubender

( ACP )

 

 

Participating Frequently
February 2, 2021

Thanks for letting me know about that font issue.

All my pdf documents are single page documents and so far, using autosize settings on the idml document as suggested, and using fit, move, geometric bounds and resize, I have working code.

      var bodyBounds = bodyFrame.geometricBounds; // bounds before resize
      bodyFrame.fit(FitOptions.FRAME_TO_CONTENT);
      bodyBounds = bodyFrame.geometricBounds; // bounds after resize
      var bodyHeight = bodyBounds[2] - bodyBounds[0];
      var bodyWidth = bodyBounds[3] - bodyBounds[1];

      var footerBounds = footerFrame.geometricBounds;
      var footerHeight = footerBounds[2] - footerBounds[0];
      var gutter = 0.0417; //  inset spacing
      var hruleHeight = 0.005; // horizontal rule

      // resize body bounds to fit footer frame, gutter and horizontal rule
      bodyBounds[2] = bodyBounds[2] + footerHeight + (gutter * 2) + hruleHeight;
      bodyFrame.geometricBounds = bodyBounds;

      // move footer frame inside bodyframe
      footerFrame.move([footerBounds[1], bodyHeight + (gutter + hruleHeight)]);
      var newBodyHeight = bodyBounds[2] - bodyBounds[0];

      // Add horizonal rule to bottom of document
      var myGraphicLine = idmlDoc.graphicLines.add({ strokeWeight: 0.75 });
      myGraphicLine.paths.item(0).pathPoints.item(0).anchor = [0, newBodyHeight - hruleHeight];
      myGraphicLine.paths.item(0).pathPoints.item(1).anchor = [bodyWidth, newBodyHeight - hruleHeight];

      // Resize page height to match bodyFrame height
      var newPageHeight = newBodyHeight;
      idmlDoc.documentPreferences.pageHeight = newPageHeight;

      // Group all page items and move the group to top left corner of page
      // This ensures all textframes appear in the printing area because they move out of it when the pageHeight is shrunk
      var myItems = idmlDoc.pageItems.everyItem();
      myItems.locked = false;
      var myGroup = idmlDoc.groups.add(myItems);
      myGroup.move([0, 0]);
      myGroup.ungroup();

An issue that has now arisen though is that the fit options isn't taking into accound the removal of unneeded textframes. If I don't have an image, I remove it like so:

if (photo_1 == "") { myTextFrames[j].remove(); }
It seems like it's removal doesn't remove it's geometric bounds from the page so fit options aren't corrected properly. 
Community Expert
January 26, 2021

Hi Alexandria5E9E,

what exact version of InDesign Server do we talk about?

If you are running version 8 and above look into textFramePreferences and enable auto size.

https://www.indesignjs.de/extendscriptAPI/indesign16/#TextFramePreference.html#d1e306186

 

There are some parameters that you can control:

autoSizingType and autoSizingReferencePoint

 

Also look into verticalBalanceColumns if you have more than one text column in the text frame.

 

Regards,
Uwe Laubender

( ACP )

Participating Frequently
January 26, 2021

At the moment I am using the 90 day trail of InDesign Server 2019 to learn how to use it for publishing 1 column, 2 column, 3 column and 6 column pdf's with varying amounts of text and images, hence the need to move items and trim/lengthen the document if there is a lot of whitespace. 

Is it possible for the document to autosize, not just textframes?

I recently discovered the concept of scaling from another forum post but don't entirely understand it's purpose and if we will benefit from using it. 

Does the version of InDesign that our designers use to create templates dictate what version of InDesign Server we should use?

Participating Frequently
January 26, 2021

I've discovered a few new things.

How to Trim Document Height: 

alert("Page Height: " + idmlDoc.documentPreferences.pageHeight);
var newPageHeight = idmlDoc.documentPreferences.pageHeight / 2;
idmlDoc.documentPreferences.pageHeight = newPageHeight;
alert("New Page Height: " + idmlDoc.documentPreferences.pageHeight);

I'd like the value from newPageHeight to be the discovered by accounting for the space taken up by all the textframes but have't figured that out yet.

How to Move/Trim a Textframe:
You can use the Script Label panel in CS4 and below and the Layers panel in CS5 and above to defined names for your textFrames. Then you can select a textframe by name and apply settings. This is what I did to move a specific textframe. However, the textframes end up outside the printing area of the document.

var myTextframe = idmlDoc.textFrames.item("Bottom_Image");
var myBounds = myTextframe.geometricBounds;
alert("oMyBloc: " + myBounds);
// Move the textframe [.move(x, y)]
myTextframe.move([myBounds[1], myPageHeight / 2]);


To trim a textframe I can modify using geometricBounds or fitOptions but my textframes still end up outside the of printing area of the document.

    // y1    x1      y2        x2
    // [0]   [1]     [2]       [3]
    // y     x      x + w     y + h

myTextframe.geometricBounds = [y, x, x+w, y+h]; 

    //FitOptions.CONTENT_TO_FRAME
    //FitOptions.CENTER_CONTENT
    //FitOptions.PROPORTIONALLY
    //FitOptions.FRAME_TO_CONTENT
    //FitOptions.FILL_PROPORTIONALLY
    //FitOptions.APPLY_FRAME_FITTING_OPTIONS

myTextframe.fit(FitOptions.CONTENT_TO_FRAME);
 




 

Loic.Aigon
Legend
January 22, 2021

You could replace values in the idml file itself, saving some resources. But for layout adjustements, that may be tough to do it at this stage. Once that said, if possible, I tend to tag with xml tags, items I need to edit in such template based projects. So my script looks for such tagged items through the xml structure  (see evaluateXPathExpression). Same stands for object removal if needed.

Regarding layout adjustments, you may prefer using geometry other fitting functions (see geometricBounds)

FWIW

 

Participating Frequently
January 25, 2021

I"ve only been working on Indesign Scripts the last few weeks so without an example I can't see how your method would work.