IDML Reference / Stories & text
Extract all text
Pull the plain text out of an IDML document — walk each story, end paragraphs at the <Br/> mark, keep forced breaks and tabs, map special characters.
Reviewed against engine v0.71.6
To extract a document's text, walk each story's Content in order, end a paragraph at every <Br/>, and map the few special characters.
In short The text of an IDML document is in its story parts. Walk each story in
document order and append the text of every Content element. A <Br/> ends the
current paragraph, so emit a newline there; a ParagraphStyleRange boundary does not end
anything on its own. Forced line breaks (U+2028) and tabs (U+0009) are already characters
in the text. Page numbers and section markers are processing instructions to replace or
drop. No style needs resolving.
The recipe
- List the stories. Each
idPkg:Storyinclude in the design map names one story part (Stories/Story_<id>.xml);StoryListon theDocumentelement lists the same ids. - Open each story part and walk it in document order. A part holds one
Story. - Append every
Contentelement's text as you reach it. Decode the XML as usual, so&and	arrive as&and a tab. - Emit a newline for every
<Br/>. It is the paragraph mark. Don't add anything atParagraphStyleRangeorCharacterStyleRangeboundaries: those are formatting changes, and a range can start mid-sentence. - Keep U+2028 and U+0009 as they are, or map U+2028 to a newline if your output has no notion of a line break inside a paragraph.
- Map the processing instructions inside
Content:<?ACE 18?>is a page number and<?ACE 19?>a section marker. Replace them with a placeholder, or drop them; the values depend on which page the text lands on.
Worked over a story with three paragraphs
The story below yields:
A first paragraph.
A second one, with a forced
line break inside it.
A third paragraph, set apart by its own SpaceBefore.Two <Br/> marks give two newlines. The forced line break is written as a newline here
too; keep it as U+2028 if you need to tell it apart. The second ParagraphStyleRange
contributes nothing but its text: it exists because the third paragraph's spacing
differs, and SpaceBefore is layout, which extraction ignores.
Three paragraphs, two marks: the ranges are formatting, the marks are structure.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="20.0">
<Story Self="ustory">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>A first paragraph.</Content>
<Br/>
<Content>A second one, with a forced
line break inside it.</Content>
<Br/>
</CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]" SpaceBefore="12">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>A third paragraph, set apart by its own SpaceBefore.</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
</Story>
</idPkg:Story>
Things to get right
- Reading order is a layout question. The list of stories says which text exists, not the order a reader meets it. For a layout's reading order, follow the text frames on each spread, page by page, and the order of frames in each thread.
- A threaded story is still one story. Text that flows through several frames is one part; extract it once. See threading and overset.
- Some text is not printed.
Notecontent, hidden conditional text and index markers are in the story but not on the page. Skip them unless you want everything. - Tables keep their text in cells. A
Tablein a run holdsCellelements, each with its ownParagraphStyleRangecontent. Walk them if you want the full copy, and decide on a separator. - Footnotes are nested text. A
Footnotein a run carries its own paragraphs; include them inline, collect them at the end, or skip them. - Inline and anchored frames are separate stories. A text frame anchored in a run names
its own
ParentStory, which is a story part of its own. - A
TextVariableInstanceshows itsResultText, the value InDesign last composed. It may be out of date for the page the text now lands on.
Frequently asked questions
Where do paragraphs end?
At every <Br/>. A ParagraphStyleRange is a run of shared formatting that can hold
several paragraphs, so adding a newline per range splits paragraphs wherever formatting
changes and doubles the breaks that are already there.
Do I need to resolve styles to extract text?
No. Extraction only needs the characters and the paragraph marks; Styles.xml is not
involved.
How are tabs written?
As the tab character inside Content, often as 	. An XML parser decodes it like any
other character reference.
What is <?ACE 18?> in my text?
A processing instruction for the page number. InDesign fills it in per page; extracted
text usually replaces it with a placeholder or drops it.
Threading and overset
How one IDML story flows through a chain of text frames via NextTextFrame and PreviousTextFrame, and what overset text is when the chain runs out of room.
Styles
How IDML keeps formatting in named, reusable styles, which kinds of style there are, which part each lives in, and how text and frames refer to them.