Search Docs
Loading...
Skip to content

Copy and Paste Formatted Text

Copy and paste text with its formatting between text blocks and other apps, and read or write a range of a text block as HTML.

6 mins
estimated time
GitHub

Text blocks read and write their formatted content as HTML. engine.block.getTextAsHTML() returns a range as HTML with inline CSS. The HTML states the color, background color, weight, italic, decorations, script style, case, font size and typeface of each run. It also states the list style, list level and alignment of each paragraph. engine.block.replaceTextWithHTML() replaces a range with the text and the formatting of HTML. This is the same format that browsers and other apps put on the clipboard.

Both calls take from and to as UTF-16 code unit offsets, with to exclusive. Without a range, outside text edit mode, both calls use the whole text. Plain text editing is in Edit Text.

Using the Built-in Copy and Paste#

In text edit mode, the system copy, cut and paste actions keep the formatting of the selected text. A copy puts both the plain text and the HTML on the clipboard, so a paste into another app also keeps the formatting. A paste reads the HTML of the clipboard. A clipboard without HTML, or with HTML that holds no text, inserts the plain text. The Paste as plain text action of the text menu, and Ctrl+Shift+V on a hardware keyboard, paste the plain text in the style of the block, as Paste and Match Style describes. Integrations that let users edit text on the canvas need no code for this.

Canva reads only the plain text of a copy from CE.SDK, because Canva ignores the formatting of other apps.

In the Android editor, outside text edit mode, text from another app pastes on the canvas as a new text block, as Copy, Cut and Paste in the canvas menu guide describes.

Copy Formatted Text Between Blocks#

Read the formatted text of the source block with engine.block.getTextAsHTML().

// Copy the whole text with every run and paragraph format
val copiedHtml = engine.block.getTextAsHTML(block = source)

Write it to another block with engine.block.replaceTextWithHTML(). HTML from getTextAsHTML() sets every format it carries, including the typeface and the font size. The typeface applies only when a typeface of the scene holds its font file. Otherwise the text keeps the typeface at the insert position, with the weight and style of the source.

// Replace the whole text of the target block with the copied text
engine.block.replaceTextWithHTML(block = target, html = copiedHtml)

To copy a part of a block, pass from and to to getTextAsHTML(). An empty range returns an empty string. To insert without replacing text, pass the same index as from and to. The example copies the red title and inserts it at the end of the headline.

// Copy only "Summer Sale" and insert it at the end of the headline
val titleHtml = engine.block.getTextAsHTML(block = source, from = 0, to = 11)
engine.block.replaceTextWithHTML(block = headline, html = titleHtml, from = 7, to = 7)

In text edit mode and without a range, both calls use the selection. replaceTextWithHTML() then moves the cursor behind the inserted text.

Paste HTML From Other Apps#

Pass HTML from the clipboard of another app, such as Google Docs, Word, a web page or an Apple app, to engine.block.replaceTextWithHTML().

// HTML as a browser or a document app puts it on the clipboard, with its font size and line height
val clipboardHtml =
"""<p style="line-height:1.5"><b style="font-size:24px">Bold</b> and <span style="color:#ff0000">red</span></p>""" +
"""<ul><li>item</li></ul>"""
engine.block.replaceTextWithHTML(block = pasted, html = clipboardHtml)

HTML from another app sets these formats:

  • Weight, italic, decorations, script style and case.
  • Text color and background color.
  • Font size, letter spacing and line height.
  • Lists, list levels and alignment.

The pasted text takes the first font family of the HTML that a typeface of the asset sources has. Without such a typeface, it keeps the typeface of the block. A CSS px is one pixel of a scene whose font size unit is the pixel, so the size reads as in the source app. A gray, black or white that covers the largest part of the pasted text is the default color of the source app. It does not replace the color of the block, and the same rule applies to the background color. A pasted paragraph without a list keeps the list of the target paragraph. Markup that is not supported keeps only its text.

Paste and Match Style#

Paste as plain text pastes only the plain text, which takes the style at the cursor, as typed text does. In text edit mode, it is in the text menu when the clipboard holds formatted text, and on Ctrl+Shift+V of a hardware keyboard. Your own input does the same with engine.block.replaceText() and the plain text of the clipboard.

Handle HTML Without Text#

When the HTML holds no text, such as a copied image, engine.block.replaceTextWithHTML() throws an EngineException whose code is EngineErrorCode.BLOCK_TEXT_HTML_HAS_NO_TEXT, and the block does not change. Catch it and insert the plain text of the clipboard with engine.block.replaceText(). The Error Catalog lists all error codes.

// A copied image puts HTML without text on the clipboard
val imageHtml = """<p><img src="https://example.com/photo.png"></p>"""
val imagePlainText = "Beach photo"
try {
engine.block.replaceTextWithHTML(block = note, html = imageHtml)
} catch (error: EngineException) {
if (error.code != EngineErrorCode.BLOCK_TEXT_HTML_HAS_NO_TEXT) throw error
// The block is unchanged, so insert the plain text of the clipboard instead
engine.block.replaceText(block = note, text = imagePlainText)
}

Scopes#

Without the text/character scope, replaceTextWithHTML() inserts only the text, with the style at the insert position. Without the fill/change scope, or when the text color is bound to a variable, no color is pasted. When the font size or the typeface of the block is bound to a variable, it is not pasted. replaceTextWithHTML() also requires the text/edit scope. Lock Design shows how to set scopes.

Troubleshooting#

Problem Cause and solution
Pasted text has the wrong typeface No typeface of the asset sources has the font family of the source app, so the text keeps the typeface of the block. Add that typeface to an asset source.
Pasted text is much smaller or larger than the block The paste keeps the font size of the source app. Use Paste as plain text to paste the text in the style of the block.
A paste into Canva has no formatting Canva reads only the plain text of other apps.
Bold or italic does not show The typeface of the block has no bold or italic font. Use a typeface with these variants.
The paste throws BLOCK.TEXT_HTML_HAS_NO_TEXT The HTML holds no text. The example catches this error and inserts the plain text with engine.block.replaceText().
The text color of the paste is not applied A gray, black or white that covers the largest part of the pasted text does not replace the color of the block. The same rule applies to the background color. A block without the fill/change scope, or with its text color bound to a variable, takes no color.
Only the text is pasted, without formatting The block does not allow the text/character scope.

API Reference#

Method Purpose
engine.block.getTextAsHTML(block=_, from=_, to=_) Get a UTF-16 range of a text block as HTML with its formatting.
engine.block.replaceTextWithHTML(block=_, html=_, from=_, to=_) Replace a UTF-16 range with the text and formatting of HTML.
engine.block.replaceText(block=_, text=_, from=_, to=_) Insert plain text, the fallback for HTML without text.

Next Steps#

  • Edit Text - Replace, remove and format text ranges.
  • Text Styling - Apply fonts, colors, alignment and other styling options.
  • Text Decorations - Add underline, strikethrough and overline decorations.
  • Lock Design - Control which users can change text and its formatting.