Skip to main content

SetStyle

The text style base method.

note

This method is not used by itself, as it only forms the basis for the ApiRun#SetStyle method which sets the selected or created style to the text.

Inherited from ApiTextPr.SetStyle.

Syntax​

expression.SetStyle(style);

expression - A variable that represents an ApiRun class.

Parameters​

NameRequired/OptionalData typeDefaultDescription
styleRequiredApiStyle | string | null | undefinedThe style which must be applied to the text character, or the name of an existing style, or undefined/null to remove the style.

Returns​

ApiTextPr

Example​

Apply a named character style to a text run in a document.

// How do I attach a predefined style to a specific run of text in a document?

// Reuse a saved set of formatting rules by linking it to a text run in a document.

let doc = Api.GetDocument();
let myNewRunStyle = doc.CreateStyle("My New Run Style", "run");
let textPr = myNewRunStyle.GetTextPr();
textPr.SetCaps(true);
textPr.SetFontFamily("Calibri Light");
let paragraph = doc.GetElement(0);
let run = Api.CreateRun();
run.AddText("This is just a sample text. ");
run.AddText("The text properties are changed and the style is added to the paragraph. ");
paragraph.AddElement(run);
run = Api.CreateRun();
run.SetStyle(myNewRunStyle);
run.AddText("This is a text run with its own style.");
paragraph.AddElement(run);