Paged.The open publishing system
The format atlas
Stories & text

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.

Tier: IntermediateIntermediateIIhow-to

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

  1. List the stories. Each idPkg:Story include in the design map names one story part (Stories/Story_<id>.xml); StoryList on the Document element lists the same ids.
  2. Open each story part and walk it in document order. A part holds one Story.
  3. Append every Content element's text as you reach it. Decode the XML as usual, so &amp; and &#9; arrive as & and a tab.
  4. Emit a newline for every <Br/>. It is the paragraph mark. Don't add anything at ParagraphStyleRange or CharacterStyleRange boundaries: those are formatting changes, and a range can start mid-sentence.
  5. 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.
  6. 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.

Stories/Story_ustory.xmlRead / the source
<?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&#x2028;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. Note content, 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 Table in a run holds Cell elements, each with its own ParagraphStyleRange content. Walk them if you want the full copy, and decide on a separator.
  • Footnotes are nested text. A Footnote in 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 TextVariableInstance shows its ResultText, 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 &#9;. 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.

On this page