ADAM DocMaker gives you the necessary tools to manage the creation and editing of
your InDesign documents. Apart from the out of the box DocMaker Studio, a robust
DocMaker API is also available to build your own custom solution from scratch.
InDesign provides the tools to create professional layouts for print. Needless to
say that the layout is also very important when editing documents using DocMaker.
Our Studio provides page-by-page previews which makes it possible to select each
individual element by clicking it.
For custom solutions, the API also provides all necessary tools to visualize each
box on the pages in the document. By using this solution, users can easily see which
textbox needs to be edited. But what if you do not need a shiny user interface?
You want to create a solution that updates text in boxes without user interaction.
How is it possible then to know which textbox to load to replace the text in it.
This post provides some possibilities to do this.
Loading by ID
The easiest way is to load your elements by ID. This is really straight forward:
| C# |
1
2
3
4
5
|
Document document;
TextElement element = (TextElement)document.GetElement("elementId");
Story story = element.Story;
story.FromHtml(htmlValue);
|
Once your element is loaded, contents is easily updated using the FromHtml method
on the Story. If you want more advanced options for editing the contents, you can
use the API to change, create or remove Paragraphs or Runs manually in the Story.
Without ID
By contents
What if you want to load your story or textelement by contents? An InDesign document
template has 5 boxes: one header, three body elements and one footer. Suppose the
text in the header is 'header', in the bodies 'body1', 'body2' and 'body3'. Let's
take a look how to load the element that contains 'header'.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Document document;
TextElement textElement = null;
List<Story> stories = document.Stories
.Where(story => story.ToText().Equals('header', StringComparison.InvariantCultureIgnoreCase))
.ToList();
if(stories.Count == 1)
{
Story story = stories[0];
// You have the desired story, so you can start editing.
// If you still need the TextElement containing this story,
// you can use this expresion.
textElement = document.GetElement(
element => element.OwnerStory == story) as TextElement;
}
|
By Coordinates
As mentioned in the introduction it is possible to draw every element on a page
preview using the API. You may not want to provide a user interface, but it is
still possible to use this information to load the top box (header) of the page
for example. Let's take a look...
| C# |
1
2
3
4
5
6
|
// Start by loading all textelements on a specified page
Page page = document.Pages[0];
IList<TextElement> elements = page.GetAllElements()
.OfType<TextElement>().ToList();
TextElement element = elements.OrderBy(e => e.Shape.Bounds.Top).First();
|
By Tag
The latest version of the DocMaker API has en extended to work with
InDesign Tags. Adobe InDesign has the ability to tag Elements, stories,
text fragments, ... With DocMaker 3.1, it is possible to read and rename tags, and
read, edit and remove attributes of these tags.
We again have the InDesign document from previous example. Suppose the same 5 boxes,
containing real (unknown) text. Each box is tagged in InDesign. The name of the
tags are 'header', 'body1', ...
In following snippet we first get all textelements. From these elements, the one
with tage 'header' is loaded.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
IList<textelement> elements =
document.EnumerateAllElements().OfType<TextElement>().ToList();
TextElement element = elements
.Where(e => (e.Tag != null) && (e.Tag.Name == "header"))
.First();
Story story = element.Story;
// In case you also need the information in the attributes in the tag.
Tag tag = element.Tag;
IDictionary<string,string> attributes = tag.Attributes;
foreach(string attributeKey in attributes.Keys)
{
string attributeValue = attributes[attributeKey];
// Process, change, ... the attributes.
}
|
In this blog we have shared some possibilities to load the desired stories or textelements
in a document. Of course there are still many other possibilities. The used snippets
are only there to give you a thought of what's possible.