Swap text and images in a template
Fill an IDML template from code — find frames by Name, replace the Content of the story a text frame points at, and put a linked Image into a graphic frame.
A template is an IDML package whose frames have names. Find the frame, follow it to its story or its image, and change only what is inside.
In short Give each slot in the template a Name in InDesign (the Layers panel names
it), so the frame carries Name="headline" or Name="photo" in the spread. For a text
slot, follow the frame's ParentStory to Stories/Story_<id>.xml and replace the text
inside its Content elements, keeping the ranges around them so the formatting stays. For
an image slot, add an <Image> with its own ItemTransform, a GraphicBounds pixel box
and a <Link> to an absolute file URI, or change the LinkResourceURI of the image already
there. Leave every other part as it is. Download the sample template.
A one-page template with two named slots: a text frame headline and an empty graphic
frame photo.
Swap the text
- Find the frame. In the spread parts, look for the
TextFramewith the slot'sName. An unnamed frame carriesName="$ID/". - Follow
ParentStory. It names the story'sSelfid; the story is inStories/Story_<id>.xml, referenced from the design map. - Replace the text, not the structure. Change the text inside the existing
Contentelements and keep theirParagraphStyleRangeandCharacterStyleRangeparents, so the new text keeps the template's styles and local formatting. For several paragraphs, separate them with<Br/>; for a line break inside a paragraph, use U+2028. - Escape the text as XML:
&for&,<for<.
If the frame is threaded to others, the story flows through all of them; there is still only one story to change. Text that no longer fits becomes overset rather than disappearing.
Swap the image
- Find the frame. Look for the
Rectangle(orOval,Polygon) with the slot'sName. InDesign marks a frame meant for a pictureContentType="GraphicType", whether it is empty or not. - If it already holds an
<Image>, change theLinkResourceURIof its<Link>. If the new picture has a different size or shape, also set the image'sGraphicBoundsto the new pixel box and adjust itsItemTransformso the picture fills the frame the way you want. - If it is empty, add an
<Image>child as the frame's last child (InDesign writes it after<Properties>and the frame's option elements): anItemTransformmapping the picture's pixels into the frame (for a picturew×hpixels filling a frameW×Hpoints wide and high, a scale ofW/wandH/h, or the smaller of the two to fit without distortion),<Properties><GraphicBounds Left="0" Top="0" Right="w" Bottom="h"/></Properties>, and a<Link>with the file'sLinkResourceURI. The full structure is on place an image in a frame. - Use an absolute URI in InDesign's spelling,
file:/path/to/photo.jpg. A relative one opens as a missing link.
Things to get right
- Change only what you mean to. Copy every part you do not touch byte for byte. A template carries fonts, styles, preferences and metadata that your program may not understand and InDesign still needs.
- Keep the ids. Do not renumber
Selfids; other parts point at them. - Do not drop the ranges. Replacing a whole
ParagraphStyleRangewith bare text loses the template's formatting, and text outside aCharacterStyleRangeis not where a reader looks for it. - A story named by no frame is dropped by InDesign. If you add a story, place it in a frame.
In Paged: the same swap can be made through Paged's editor or scripting, and saving writes it back by patching the original package: only the changed story and spread are rewritten. Tracked
Frequently asked questions
How do I find a particular frame in an IDML template?
By its Name, set in InDesign's Layers panel and written as the Name attribute on the frame
in the spread part. Unnamed frames carry Name="$ID/". Script labels, stored as
<Properties><Label><KeyValuePair …/></Label></Properties> on the frame, are another way to
tag slots.
Why replace the text inside Content rather than the whole story?
Because the ranges around Content carry the formatting: the paragraph style, the character
style and any local overrides. Keeping them keeps the template's look.
What happens if the new text is longer than the frame? The text that does not fit becomes overset: it stays in the story but is not shown until the frame grows or another frame is threaded. See threading and overset.
Do I need to embed the new image? No. A link to the file is enough, provided the URI is absolute and the file is where it points when InDesign opens the package.
Generate an IDML file from code
The minimum IDML package a program has to write — mimetype first and uncompressed, META-INF/container.xml, the design map, resources, spreads and stories.
Read which InDesign version wrote a file
Find the InDesign build that wrote an IDML file in the <?aid ?> product field of designmap.xml, and the document-model revision in DOMVersion.