Skip to main content

SetColor

Sets the text color to the current text run.

Syntax​

expression.SetColor(color);

expression - A variable that represents an ApiRichTextPr class.

Parameters​

NameRequired/OptionalData typeDefaultDescription
colorRequiredApiColorNo description provided.

Returns​

ApiRichTextPr

Example​

Change the color of text in a free text annotation in a PDF.

// How do I change text color in a PDF?

// Apply a color to text formatting in a PDF.

let doc = Api.GetDocument();
let freeTextAnnot = Api.CreateFreeTextAnnot([160, 50, 360, 135]);
let page = doc.GetPage(0);
page.AddObject(freeTextAnnot);
freeTextAnnot.SetIntent("freeTextCallout");
freeTextAnnot.SetCallout([{x: 161, y: 51}, {x: 249, y: 125}, {x: 261, y: 125}]);
freeTextAnnot.SetRectDiff([100, 64, 0.5, 0.5]);

let richContent = freeTextAnnot.GetContent();
let para = richContent.GetElement(0);
let run = para.GetElement(0);
run.AddText("Some example text");

let textPr = run.GetTextPr();
textPr.SetColor(Api.RGB(255, 0, 0));

let color = textPr.GetColor();

console.log(`We set color to {r:${color["r"]}, g:${color["g"]}, b:${color["b"]}} for rich text properties`);