Skip to main content

Document Elements

  • 3 minutes to read

The Rich Text Editor’s document property returns a RichEditDocument object. Use this object’s API to access a sub-document and its structural elements on the client.

Access a Sub-Document

A document’s content is divided into individual parts called sub-documents. The position of the cursor indicates the active sub-document. The client commands and the selection-related API allow you to change the active sub-document and/or to modify its content.

Use the RichEditDocument‘s properties to get a list of all sub-documents in a document, main or active sub-document, or a sub-document by its ID.

// Gets the active sub-document
var activeSubDocument = richEdit.document.activeSubDocument;
// Gets the main sub-document body
var mainSubDocument = richEdit.document.mainSubDocument;
// Gets all sub-documents
var subDocuments = richEdit.document.subDocuments;
// Gets sub-document that has the id 0
var subDocument = richEdit.document.getSubDocumentById(0);

The SubDocument class object denotes a sub-document. Use the object’s properties to get information about a sub-document.

// Gets information about the main sub-document
var id = richEdit.document.mainSubDocument.id;
var length = richEdit.document.mainSubDocument.length;
var text = richEdit.document.mainSubDocument.text;
var type = richEdit.document.mainSubDocument.type;

Access a Structural Element

A SubDocument object contains information about the sub-document’s structural elements.

The table below lists properties that return information about the current state of the elements. The information is only relevant at the time you receive it because it is not updated when any further changes are made. These properties have the Info postfix to indicate that the returned objects contain only the current information about elements.

Property Description
bookmarksInfo Returns information about bookmarks in the sub-document.
fieldsInfo Returns information about fields in the sub-document.
floatingPicturesInfo Returns information about floating pictures in the sub-document.
floatingTextBoxesInfo Returns information about floating text boxes in the sub-document.
inlinePicturesInfo Returns information about in-line pictures in the sub-document.
paragraphsInfo Returns information about paragraphs in the sub-document.
sectionsInfo Returns information about sections in the main sub-document.
tablesInfo Returns information about tables in the sub-document.

Use the following methods to find a sub-document’s elements that match a certain condition:

Refer to the following section for more information about structural document elements: Document Structure.

Examples

Refer to the following section for more examples: Common Use Cases.

Insert Section Numbers

The following example shows how to insert text at the beginning of each section of the main sub-document. The sectionsInfo property is called for each iteration of the loop to get the current information about sections.

var myDoc = richEdit.document;
// Sets the main sub-document as active
richEdit.selection.setMainSubDocumentAsActive();
for (var sectionIndex = 0; sectionIndex < myDoc.sectionsInfo.length; sectionIndex++) {   
    // Positions the cursor at the beginning of the current section
    richEdit.selection.setSelection(myDoc.sectionsInfo[sectionIndex].start);
    // Inserts text at the cursor position
    richEdit.commands.insertText.execute("Section " + (sectionIndex + 1) + '\n';);
}

The following example shows how to use the fieldsInfo property to get URLs of all hyperlinks in the active sub-document.

var fields = richEdit.document.activeSubDocument.fieldsInfo;
var hyperlinkUrls = [];
for (var i = 0; i < fields.length; i++) {
    var field = fields[i];
    if(field.isHyperlink && field.hyperlinkUri)
        hyperlinkUrls.push(field.hyperlinkUri);
}

Change Table Style Settings in a Sub-Document

The following example shows how to access tables in the active sub-document and change their style settings:

// Gets a table style
var tableStyle = richEdit.document.tableStylesInfo[0];
// Gets all the active sub-document tables
var tables = richEdit.document.activeSubDocument.tablesInfo;
for (var tableIndex = 0; tableIndex < tables.length; tableIndex++) {
    // Selects the current table and changes its style
    richEdit.selection.setSelection(tables[tableIndex].interval);
    richEdit.commands.changeTableStyle.execute(tableStyle);
}