Using Text
Text is an essential part of any design. Let's explore how to use all the available APIs to create and style it.
Creating Text
The editor.createText()
method doesn't accept any parameters and returns a brand new TextNode
. The actual textual content starts as empty and is found in its fullContent.text
property.
Example
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Create a new TextNodeconst textNode = editor.createText();// Set the text contenttextNode.fullContent.text = "Hello,\nWorld!";// Center the text on the pageconst insertionParent = editor.context.insertionParent;textNode.setPositionInParent({ x: insertionParent.width / 2, y: insertionParent.height / 2 },{ x: 0, y: 0 });// Add the TextNode to the documentinsertionParent.children.append(textNode);// Get the text contentconsole.log("Text: ", textNode.fullContent.text);
The text is created with the default styles (Source Sans 3, 100pt, black). Use \n
or \r
to add a line break.
Replacing Text
The text content of a TextNode
can be replaced by setting the fullContent.text
property.
Example
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frameconst selectedTextNode = editor.context.selection[0];selectedTextNode.fullContent.text = "Something else";
Applying Character Styles
Text styles can be applied to a TextNode
using the fullContent.applyCharacterStyles()
method, which applies one or more styles to the characters in the given range, leaving any style properties that were not specified unchanged.
The styles are defined by the CharacterStylesInput
interface; the properties that can be set are:
- color
- font (please see the Using Fonts section)
- fontSize
- letterSpacing
- underline
The range is an object with the start
and length
properties.
Style Ranges and Text edits
For the moment, replacing the fullContent.text
will result in applying the style from the first range to the whole text. This behavior is subject to change in future releases.
Please note that applyCharacterStyles()
is only one way to set styles; you can also use the characterStyleRanges
property, which supports both getting and setting styles, as described here.
Example: Setting Styles in a range
Let's change the styles for the first three characters of a TextNode.
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];// Apply character styles to the first three letterstextNode.fullContent.applyCharacterStyles({color: { red: 0, green: 0.4, blue: 0.8, alpha: 1 },fontSize: 240,letterSpacing: 10,underline: true,},{start: 0,length: 3,});
The applyCharacterStyles()
method is not the only one that allows you to set styles; you can also use the characterStyleRanges
property, which supports both getting and setting styles.
Example: Getting all Styles
To get the complete list of text character styles, you can use the fullContent.characterStyleRanges
property, which returns an array of CharacterStylesRange
elements.
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];const contentModel = textNode.fullContent;// Get the array of character stylesconst existingStyles = contentModel.characterStyleRanges;// Edit some propertiesexistingStyles[0].fontSize = 10;// Reassign the array to apply the style changescontentModel.characterStyleRanges = existingStyles;
Example: Setting all Styles
You can also use the characterStyleRanges
property to set individual ranges or them all. It's always best to get the array, modify it, and then reassign it.
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];const contentModel = textNode.fullContent;// Get the array of character stylesconst existingStyles = contentModel.characterStyleRanges;// Edit some properties: the font size of all stylesexistingStyles.forEach((style) => {style.fontSize = 50;});// Alternatively, you could set the properties for a specific style range// existingStyles[0].fontSize = 50;// Reassign the array to apply the style changescontentModel.characterStyleRanges = existingStyles;
Using Fonts
In the Adobe Express Document API, Fonts are part of the Character Styles; we're treating them separately here for clarity. Similarly to the color and other properties, you can use individual CharacterStylesRange
items from the CharacterStyleRanges
array as Font getters and setters, or use the applyCharacterStyles()
method to apply a Font style to a specific range.
The only caveat is that you cannot set the font as an Object literal, like, e.g., colors; fonts must be of type AvailableFont
, and are instantiated from the fonts
object (imported from the "express-document-sdk"
) using the asynchronous fromPostscriptName()
method.
Copied to your clipboard// Always✅ const font = await fonts.fromPostscriptName("SourceSans3-Bold");// Won't work❌ const font = {availableForEditing: true,isPremium: false,family: "Source Sans 3",postscriptName: "SourceSans3-Bold",style: "Bold",}
You can get PostScript names by setting different text fonts in the Adobe Express UI; then, log and inspec the font
property of characterStyleRange
, as seen here.
Remember that the fromPostscriptName()
method is asynchronous. The promise resolves to an AvailableFont
instance only for fonts that the user has permission to use for editing content; otherwise, it will resolve to undefined
.
Example: Setting Fonts in a range.
Let's now change the font of the first three characters in a TextNode. Please note that although you're allowed to set the font as the only style, the font object itself must contain all the properties, as the following code snippet demonstrates.
Copied to your clipboard// sandbox/code.jsimport { editor, fonts } from "express-document-sdk"; // 👈 fonts import// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];// Getting a new font objectconst lato = await fonts.fromPostscriptName("Lato-Light");if (!lato) return; // in case the user isn't entitled to use this font// ⚠️ Queueing the editeditor.queueAsyncEdit(() => {textNode.fullContent.applyCharacterStyles({ font: lato, fontSize: 24 },{ start: 0, length: 3 });});
Asynchronous operations
Queuing the applyCharacterStyles()
method is necessary because fromPostscriptName()
is asynchronous. This ensures the edit is properly tracked for saving and undo. You can read more about this in the queueAsyncEdit() reference.
Example: Getting all Fonts
A font, regardless of whether accessed via CharacterStylesRange
or executing fromPostscriptName()
, exposes the following properties:
isPremium
: boolean, indicating whether the font is a Premium Adobe font.availableForEditing
: boolean, indicating whether the user has access or licensing permissions to create or edit content with this font.family
: string, the font family name, as you would find in the Text panel's UI.postscriptName
: string, the PostScript name of the font.style
: string, the style of the font (e.g., "Regular", "Bold", "Italic").
You can log font
and inspect it to find the actual PostScript name.
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];const contentModel = textNode.fullContent;// Get the array of character stylesconst existingStyles = contentModel.characterStyleRanges;// Log the font of the first styleconsole.log(existingStyles[0].font);// {// isPremium: false// availableForEditing: true// family: "Source Sans 3"// postscriptName: "SourceSans3-Regular"// style: "Regular"// }
Example: Setting all Fonts
Similarly to what we've seen with other styles, you can set the font in a range by reassigning the characterStyleRanges
array.
Copied to your clipboard// sandbox/code.jsimport { editor, fonts } from "express-document-sdk";// Assuming the user has selected a text frameconst textNode = editor.context.selection[0];const contentModel = textNode.fullContent;const sourceSansBold = await fonts.fromPostscriptName("SourceSans3-Bold");if (!sourceSansBold) return;// Get the array of character stylesconst existingStyles = contentModel.characterStyleRanges;// Set the font for all stylesexistingStyles.forEach((style) => {style.font = sourceSansBold;});// Alternatively, you could set the font for a specific style range// existingStyles[0].font = sourceSansBold;// Reassign the array to apply the style changeseditor.queueAsyncEdit(() => {contentModel.characterStyleRanges = existingStyles;});
Since we're dealing with asynchronous operations, we're queuing the edit to ensure it's properly tracked for saving and undo, as we did for setting other styles
Dealing with Text Flow
With the introduction of "Text Flow" in Adobe Express (allowing content to move freely between multiple text frames), the concept of a text node had to be separated from text content.
The fullContent
property points to a TextContentModel
object, which contains the actual text content that multiple TextNode
instances can share.
Example
Copied to your clipboard// sandbox/code.jsimport { editor } from "express-document-sdk";// Assuming the user has selected a text frame that contains// text spanning to multiple text nodesconst selectedTextNode = editor.context.selection[0];// Log all the text nodes that share the same TextContentModelfor (const textNode of selectedTextNode.fullContent.allTextNodes) {console.log(textNode);}