Font
本文介绍与文字文档Font相关的API。
Font
ActiveDocument.Range(Start, End).Font
获取字体对象。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; }
属性
ActiveDocument.Range(Start, End).Font.Color
通过Color
属性,您可以设置字体对象的颜色,该颜色可为RGB函数创建的颜色值。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font.Color
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; //设置该字体的颜色 font.Color = '#228B22'; }
ActiveDocument.Range(Start, End).Font.ColorIndex
通过ColorIndex
属性,您可以设置字体对象的颜色,该颜色可为WdColorIndex中的值。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font.ColorIndex = WdColorIndex
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; //设置该字体的颜色 font.ColorIndex = 2; }
ActiveDocument.Range(Start, End).Font.HighLight
通过HighLight
属性,您可以设置字体对象的高亮颜色。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font.HighLight
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; //设置该字体的高亮颜色 font.HighLight = '#228B22'; }
ActiveDocument.Range(Start, End).Font.Name
通过Name
属性,您可以设置字体对象的类型。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font.Name
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; //设置该字体的类型 font.Name = '宋体'; }
ActiveDocument.Range(Start, End).Font.Size
通过Size
属性,您可以设置字体对象的大小。
JS-SDK V1.1.10及以上版本支持此功能。
语法
表达式.ActiveDocument.Range(Start, End).Font.Size
或者
表达式.ActiveDocument.ActiveWindow.Selection.Font
表达式:文档类型应用对象
示例
async function example() { await instance.ready(); const app = instance.Application; //获取字体对象 const font = await app.ActiveDocument.Range(0, 20).Font; //设置该字体的大小 font.Size = 30; }
ActiveDocument.Selection.Font.Underline
通过Underline
属性,您可以设置或获取选中内容的下划线类型。
语法
表达式.ActiveDocument.Selection.Font.Underline
表达式:文档类型应用对象
参数
WdUnderline表示下划线类型
返回值
返回下划线类型WdUnderline,无下划线时返回
null
,多下划线混合时返回wdUndefined
。示例
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; //设置下划线类型为粗波浪线 app.ActiveDocument.Selection.Font.Underline = app.Enum.WdUnderline.wdUnderlineWavyHeavy //获取下划线类型 const underlineType = await app.ActiveDocument.Selection.Font.Underline console.log('underlineType: ', underlineType) }
ActiveDocument.Selection.Font.UnderlineColor
通过UnderlineColor
属性,您可以设置或获取选中内容的下划线类型。
语法
表达式.ActiveDocument.Selection.Font.UnderlineColor
表达式:文档类型应用对象
参数
16进制的颜色值,例如
'#FF0000'
返回值
默认值为
'#000000'
,无下划线时返回null
,多颜色混合时返回wdUndefined
。示例
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; //设置下划线颜色为红色 app.ActiveDocument.Selection.Font.UnderlineColor = '#FF0000' //获取下划线颜色 const underlineColor = await app.ActiveDocument.Selection.Font.UnderlineColor console.log('underlineColor: ', underlineColor) }
ActiveDocument.Selection.Font.Italic
通过Italic
属性,您可以设置或获取选中内容为斜体。
语法
表达式.ActiveDocument.Selection.Font.Italic
表达式:文档类型应用对象
参数
true
表示斜体,false
表示非斜体返回值
true
表示斜体,false
表示非斜体,wdUndefined表示混合。示例
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; //设置内容为斜体 app.ActiveDocument.Selection.Font.Italic = true //检查内容是否为斜体 const italic = await app.ActiveDocument.Selection.Font.Italic console.log('italic: ', italic) }
ActiveDocument.Selection.Font.Bold
通过Bold
属性,您可以设置或获取选中内容为粗体。
语法
表达式.ActiveDocument.Selection.Font.Bold
表达式:文档类型应用对象
参数
true
表示粗体,false
表示非粗体返回值
true
表示粗体,false
表示非粗体,wdUndefined
表示混合。示例
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; //设置内容为粗体 app.ActiveDocument.Selection.Font.Bold = true //检查内容是否为粗体 const bold = await app.ActiveDocument.Selection.Font.Bold console.log('bold: ', bold) }