Font

Updated at:

This topic describes the APIs for the Font object in text documents.

Font

ActiveDocument.Range(Start, End).Font

Retrieves a font object.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
    }

Properties

ActiveDocument.Range(Start, End).Font.Color

Use the Color property to set the color of a font object. The color can be a value created by the RGB function.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font.Color

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
      
      // Set the color of the font.
      font.Color = '#228B22';
    }

ActiveDocument.Range(Start, End).Font.ColorIndex

Use the ColorIndex property to set the color of a font object. The color can be a value from WdColorIndex.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font.ColorIndex = WdColorIndex

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
    
      // Set the color of the font.
      font.ColorIndex = 2;
    }

ActiveDocument.Range(Start, End).Font.HighLight

Use the HighLight property to set the highlight color of a font object.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font.HighLight

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
      
      // Set the highlight color of the font.
      font.HighLight = '#228B22';
    }

ActiveDocument.Range(Start, End).Font.Name

You can use the Name property to set the font type.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font.Name

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
      
      // Set the type of the font.
      font.Name = 'SimSun';
    }

ActiveDocument.Range(Start, End).Font.Size

Use the Size property to set the size of a font object.

Important

This feature is supported in JS-SDK V1.1.10 and later.

  • Syntax

    expression.ActiveDocument.Range(Start, End).Font.Size

    or expression.ActiveDocument.ActiveWindow.Selection.Font

    expression: The application object for the document type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Get the font object.
      const font = await app.ActiveDocument.Range(0, 20).Font;
      
      // Set the size of the font.
      font.Size = 30;
    }

ActiveDocument.Selection.Font.Underline

Use the Underline property to set or retrieve the underline type of the selected content.

  • Syntax

    expression.ActiveDocument.Selection.Font.Underline

    expression: The application object for the document type.

  • Parameters

    WdUnderline specifies the underline type.

  • Return value

    Returns the underline type WdUnderline. Returns null if no underline exists. Returns wdUndefined if the selection contains multiple underline types.

  • Example

    //@file=base.docx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Set the underline type to a heavy wavy line.
      app.ActiveDocument.Selection.Font.Underline = app.Enum.WdUnderline.wdUnderlineWavyHeavy
    
      // Get the underline type.
      const underlineType = await app.ActiveDocument.Selection.Font.Underline
    
      console.log('underlineType: ', underlineType)
    }

ActiveDocument.Selection.Font.UnderlineColor

Use the UnderlineColor property to set or retrieve the underline color of the selected content.

  • Syntax

    expression.ActiveDocument.Selection.Font.UnderlineColor

    expression: The application object for the document type.

  • Parameters

    A hexadecimal color value, such as '#FF0000'.

  • Return value

    The default value is '#000000'. Returns null if no underline exists. Returns wdUndefined if the selection contains multiple colors.

  • Example

    //@file=base.docx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Set the underline color to red.
      app.ActiveDocument.Selection.Font.UnderlineColor = '#FF0000'
    
      // Get the underline color.
      const underlineColor = await app.ActiveDocument.Selection.Font.UnderlineColor
    
      console.log('underlineColor: ', underlineColor)
    }

ActiveDocument.Selection.Font.Italic

Use the Italic property to set or determine whether the selected content is italic.

  • Syntax

    expression.ActiveDocument.Selection.Font.Italic

    expression: The application object for the document type.

  • Parameters

    true indicates italic and false indicates not italic.

  • Return value

    true for italic, false for non-italic, and wdUndefined for mixed formatting.

  • Example

    //@file=base.docx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Set the content to italic.
      app.ActiveDocument.Selection.Font.Italic = true
    
      // Check whether the content is italic.
      const italic = await app.ActiveDocument.Selection.Font.Italic
    
      console.log('italic: ', italic)
    }

ActiveDocument.Selection.Font.Bold

Use the Bold property to set or determine whether the selected content is bold.

  • Syntax

    expression.ActiveDocument.Selection.Font.Bold

    expression: The application object for the document type.

  • Parameters

    true indicates bold and false indicates not bold.

  • Return value

    A value of true indicates that the text is bold, false indicates that the text is not bold, and wdUndefined indicates that the text has mixed formatting.

  • Example

    //@file=base.docx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
    
      // Set the content to bold.
      app.ActiveDocument.Selection.Font.Bold = true
    
      // Check whether the content is bold.
      const bold = await app.ActiveDocument.Selection.Font.Bold
    
      console.log('bold: ', bold)
    }