Word.Font class
表示字体。
注解
使用方
- Word.正文:字体
- Word.ConditionalStyle: font
- Word.ContentControl: font
- Word.列表:getLevelFont
- Word.ListLevel: font
- Word.段落:字体
- Word.范围:字体
- Word.按选定内容:字体
- Word.样式:字体
- Word.表:字体
- Word.TableRow:字体
示例
// Change the font color
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to change the font color of the current selection.
selection.font.color = 'blue';
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The font color of the selection has been changed.');
});
属性
| all |
指定字体是否格式化为全大写字母,这将使小写字母显示为大写字母。 可能值包括:
|
| bold | 指定字体是否为粗体。
|
| bold |
指定在从右向左的语言文档中字体是否格式化为粗体。 可能值包括:
|
| borders | 返回 |
| color | 指定字体的颜色。 可以以“#RRGGBB”格式或颜色名称提供值。 |
| color |
指定表示 |
| color |
指定从右到左语言文档中对象 |
| context | 与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。 |
| contextual |
指定是否为字体启用上下文替代项。 |
| diacritic |
指定对象音 |
| disable |
指定 Microsoft Word 是否忽略相应 |
| double |
指定字体是否具有双删除线。
|
| emboss | 指定字体是否格式化为浮雕字体。 可能值包括:
|
| emphasis |
指定表示 |
| engrave | 指定字体是否格式化为雕刻字体。 可能值包括:
|
| fill | 返回一个 |
| glow | 返回一个 |
| hidden | 指定字体是否已标记为隐藏。
|
| highlight |
指定突出显示颜色。 若要设置它,请使用“#RRGGBB”格式或颜色名称的值。 要删除突出显示颜色,请将其设置为 |
| italic | 指定字体是否为斜体。
|
| italic |
指定从右向左的语言文档中字体是否为斜体。 可能值包括:
|
| kerning | 指定 Microsoft Word 自动调整字距调整的最小字号。 |
| ligature | 指定对象的 |
| line | 返回指定 |
| name | 指定字体的名称。 |
| name |
指定用于字符代码从 0 (零) 到 127) 的拉丁文本 (字符的字体。 |
| name |
指定从右向左语言文档中的字体名称。 |
| name |
指定东亚字体名称。 |
| name |
指定用于代码为 128 至 255 的字符的字体。 |
| number |
指定 OpenType 字体的数字形式设置。 |
| number |
指定字体的数字间距设置。 |
| outline | 指定字体的格式是否为轮廓。 可能值包括:
|
| position | 指定以点) 相对于基线的点 (的位置。 |
| reflection | 返回表示 |
| scaling | 指定应用于字体的缩放百分比。 |
| shadow | 指定字体是否格式化为阴影字体。 可能值包括:
|
| size | 指定字号(以磅为单位)。 |
| size |
指定从右到左文本的字号(以磅为单位)。 |
| small |
指定字体是否格式化为小型大写,这会使小写字母显示为小型大写字母。 可能值包括:
|
| spacing | 指定字符之间的间距。 |
| strike |
指定字体是否具有删除线。
|
| stylistic |
指定字体的样式集。 |
| subscript | 指定字体是否为下标。
|
| superscript | 指定字体是否为上标。
|
| text |
返回 |
| text |
返回指定 |
| three |
返回包含 |
| underline | 指定字体的下划线类型。
|
| underline |
指定对象下 |
方法
| decrease |
将字号减小为下一个可用的字号。 |
| increase |
将字号增大为下一个可用的字号。 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| reset() | 删除手动设置的字符格式。 |
| set(properties, options) | 同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。 |
| set(properties) | 基于现有加载的对象,同时在对象上设置多个属性。 |
| set |
将指定的字体格式设置为活动文档和所有基于活动模板创建的新文档的默认格式。 |
| toJSON() | 覆盖 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 |
属性详细信息
allCaps
指定字体是否格式化为全大写字母,这将使小写字母显示为大写字母。 可能值包括:
true:所有文本都具有 “全部大写” 属性。false:所有文本均不具有 “全部大写” 属性。null:如果部分(但不是全部)文本具有 “全部大写 ”属性,则返回。
allCaps: boolean;
属性值
boolean
注解
bold
指定字体是否为粗体。
true 如果字体格式设置为粗体,否则为 false.
bold: boolean;
属性值
boolean
注解
示例
// Bold format text
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to make the current selection bold.
selection.font.bold = true;
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The selection is now bold.');
});
boldBidirectional
指定在从右向左的语言文档中字体是否格式化为粗体。 可能值包括:
true:所有文本均为粗体。false:没有任何文本是粗体的。null:如果部分(但不是全部)文本为粗体,则返回。
boldBidirectional: boolean;
属性值
boolean
注解
borders
返回 BorderUniversalCollection 表示字体所有边框的对象。
readonly borders: Word.BorderUniversalCollection;
属性值
注解
color
指定字体的颜色。 可以以“#RRGGBB”格式或颜色名称提供值。
color: string;
属性值
string
注解
示例
// Change the font color
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to change the font color of the current selection.
selection.font.color = 'blue';
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The font color of the selection has been changed.');
});
colorIndex
指定表示 ColorIndex 字体颜色的值。
colorIndex: Word.ColorIndex | "Auto" | "Black" | "Blue" | "Turquoise" | "BrightGreen" | "Pink" | "Red" | "Yellow" | "White" | "DarkBlue" | "Teal" | "Green" | "Violet" | "DarkRed" | "DarkYellow" | "Gray50" | "Gray25" | "ClassicRed" | "ClassicBlue" | "ByAuthor";
属性值
Word.ColorIndex | "Auto" | "Black" | "Blue" | "Turquoise" | "BrightGreen" | "Pink" | "Red" | "Yellow" | "White" | "DarkBlue" | "Teal" | "Green" | "Violet" | "DarkRed" | "DarkYellow" | "Gray50" | "Gray25" | "ClassicRed" | "ClassicBlue" | "ByAuthor"
注解
colorIndexBidirectional
指定从右到左语言文档中对象 Font 的颜色。
colorIndexBidirectional: Word.ColorIndex | "Auto" | "Black" | "Blue" | "Turquoise" | "BrightGreen" | "Pink" | "Red" | "Yellow" | "White" | "DarkBlue" | "Teal" | "Green" | "Violet" | "DarkRed" | "DarkYellow" | "Gray50" | "Gray25" | "ClassicRed" | "ClassicBlue" | "ByAuthor";
属性值
Word.ColorIndex | "Auto" | "Black" | "Blue" | "Turquoise" | "BrightGreen" | "Pink" | "Red" | "Yellow" | "White" | "DarkBlue" | "Teal" | "Green" | "Violet" | "DarkRed" | "DarkYellow" | "Gray50" | "Gray25" | "ClassicRed" | "ClassicBlue" | "ByAuthor"
注解
context
contextualAlternates
diacriticColor
指定对象音 Font 调符号要使用的颜色。 可以用“#RRGGBB”格式提供值。
diacriticColor: string;
属性值
string
注解
disableCharacterSpaceGrid
指定 Microsoft Word 是否忽略相应Font对象的每行字符数。
disableCharacterSpaceGrid: boolean;
属性值
boolean
注解
doubleStrikeThrough
指定字体是否具有双删除线。
true 如果字体格式设置为双删除线文本,否则为 false.
doubleStrikeThrough: boolean;
属性值
boolean
注解
emboss
指定字体是否格式化为浮雕字体。 可能值包括:
true:所有文本均经过浮雕处理。false:没有任何文本浮雕。null:如果部分(但不是全部)文本是浮雕的,则返回。
emboss: boolean;
属性值
boolean
注解
emphasisMark
指定表示 EmphasisMark 字符或指定字符串的强调标记的值。
emphasisMark: Word.EmphasisMark | "None" | "OverSolidCircle" | "OverComma" | "OverWhiteCircle" | "UnderSolidCircle";
属性值
Word.EmphasisMark | "None" | "OverSolidCircle" | "OverComma" | "OverWhiteCircle" | "UnderSolidCircle"
注解
engrave
指定字体是否格式化为雕刻字体。 可能值包括:
true:雕刻所有文字。false:未雕刻任何文本。null:如果雕刻了部分(但不是全部)文本,则返回。
engrave: boolean;
属性值
boolean
注解
fill
返回一个 FillFormat 对象,该对象包含文本区域所用字体的填充格式属性。
readonly fill: Word.FillFormat;
属性值
注解
glow
返回一个 GlowFormat 对象,该对象表示文本区域使用的字体的发光格式。
readonly glow: Word.GlowFormat;
属性值
注解
hidden
highlightColor
指定突出显示颜色。 若要设置它,请使用“#RRGGBB”格式或颜色名称的值。 要删除突出显示颜色,请将其设置为 null。 返回的突出显示颜色可以是“#RRGGBB”格式,混合突出显示颜色为空字符串,或 null 无突出显示颜色。 注意:Office for Windows 桌面版中仅提供默认突出显示颜色。 它们是“黄色”、“石灰”、“绿松石色”、“粉色”、“蓝色”、“红色”、“深蓝色”、“青色”、“绿色”、“紫色”、“深红色”、“橄榄色”、“灰色”、“浅灰色”和“黑色”。 在 Office for Windows 桌面版中运行加载项时,其他任何颜色在应用于字体时都转换为最接近的颜色。
highlightColor: string;
属性值
string
注解
示例
// Highlight selected text
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to highlight the current selection.
selection.font.highlightColor = '#FFFF00'; // Yellow
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The selection has been highlighted.');
});
italic
italicBidirectional
指定从右向左的语言文档中字体是否为斜体。 可能值包括:
true:所有文本均为斜体。false:所有文本均不以斜体表示。null:如果部分(但不是全部)文本为斜体,则返回。
italicBidirectional: boolean;
属性值
boolean
注解
kerning
ligature
指定对象的 Font 连字设置。
ligature: Word.Ligature | "None" | "Standard" | "Contextual" | "StandardContextual" | "Historical" | "StandardHistorical" | "ContextualHistorical" | "StandardContextualHistorical" | "Discretional" | "StandardDiscretional" | "ContextualDiscretional" | "StandardContextualDiscretional" | "HistoricalDiscretional" | "StandardHistoricalDiscretional" | "ContextualHistoricalDiscretional" | "All";
属性值
Word.Ligature | "None" | "Standard" | "Contextual" | "StandardContextual" | "Historical" | "StandardHistorical" | "ContextualHistorical" | "StandardContextualHistorical" | "Discretional" | "StandardDiscretional" | "ContextualDiscretional" | "StandardContextualDiscretional" | "HistoricalDiscretional" | "StandardHistoricalDiscretional" | "ContextualHistoricalDiscretional" | "All"
注解
line
返回指定 LineFormat 行的格式的对象。
readonly line: Word.LineFormat;
属性值
注解
name
指定字体的名称。
name: string;
属性值
string
注解
示例
// Change the font name
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to change the current selection's font name.
selection.font.name = 'Arial';
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The font name has changed.');
});
nameAscii
nameBidirectional
nameFarEast
nameOther
numberForm
指定 OpenType 字体的数字形式设置。
numberForm: Word.NumberForm | "Default" | "Lining" | "OldStyle";
属性值
Word.NumberForm | "Default" | "Lining" | "OldStyle"
注解
numberSpacing
指定字体的数字间距设置。
numberSpacing: Word.NumberSpacing | "Default" | "Proportional" | "Tabular";
属性值
Word.NumberSpacing | "Default" | "Proportional" | "Tabular"
注解
outline
指定字体的格式是否为轮廓。 可能值包括:
true:将突出显示所有文本。false:未突出任何文本。null:如果部分文本(但不是全部)被轮廓显示,则返回。
outline: boolean;
属性值
boolean
注解
position
reflection
返回表示 ReflectionFormat 形状反射格式的对象。
readonly reflection: Word.ReflectionFormat;
属性值
注解
scaling
shadow
指定字体是否格式化为阴影字体。 可能值包括:
true:所有文本都有阴影。false:所有文本均不带有阴影。null:如果部分(但不是全部)文本带有阴影,则返回。
shadow: boolean;
属性值
boolean
注解
size
指定字号(以磅为单位)。
size: number;
属性值
number
注解
示例
// Change the font size
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to change the current selection's font size.
selection.font.size = 20;
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The font size has changed.');
});
sizeBidirectional
smallCaps
指定字体是否格式化为小型大写,这会使小写字母显示为小型大写字母。 可能值包括:
true:所有文本都具有 小型大写 属性。false:没有文本具有 小型大写 属性。null:如果部分(但不是全部)文本具有 小型大写 属性,则返回。
smallCaps: boolean;
属性值
boolean
注解
spacing
strikeThrough
指定字体是否具有删除线。
true 如果字体格式设置为删除线文本,否则为 false。
strikeThrough: boolean;
属性值
boolean
注解
示例
// Strike format text
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to strikethrough the font of the current selection.
selection.font.strikeThrough = true;
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The selection now has a strikethrough.');
});
stylisticSet
指定字体的样式集。
stylisticSet: Word.StylisticSet | "Default" | "Set01" | "Set02" | "Set03" | "Set04" | "Set05" | "Set06" | "Set07" | "Set08" | "Set09" | "Set10" | "Set11" | "Set12" | "Set13" | "Set14" | "Set15" | "Set16" | "Set17" | "Set18" | "Set19" | "Set20";
属性值
Word.StylisticSet | "Default" | "Set01" | "Set02" | "Set03" | "Set04" | "Set05" | "Set06" | "Set07" | "Set08" | "Set09" | "Set10" | "Set11" | "Set12" | "Set13" | "Set14" | "Set15" | "Set16" | "Set17" | "Set18" | "Set19" | "Set20"
注解
subscript
superscript
textColor
返回 ColorFormat 表示字体颜色的对象。
readonly textColor: Word.ColorFormat;
属性值
注解
textShadow
返回指定 ShadowFormat 字体阴影格式的对象。
readonly textShadow: Word.ShadowFormat;
属性值
注解
threeDimensionalFormat
返回包含 ThreeDimensionalFormat 字体的 3D (3D) 效果格式设置属性的对象。
readonly threeDimensionalFormat: Word.ThreeDimensionalFormat;
属性值
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/25-paragraph/apply-bevel-to-text.yaml
if (!Office.context.requirements.isSetSupported("WordApiDesktop", "1.3")) {
console.error("This sample requires WordApiDesktop 1.3 or later.");
return;
}
await Word.run(async (context) => {
const paragraph: Word.Paragraph = context.document.body.insertParagraph(
"Beveled text",
Word.InsertLocation.start,
);
paragraph.font.size = 48;
paragraph.font.bold = true;
const threeDimensionalFormat: Word.ThreeDimensionalFormat = paragraph.font.threeDimensionalFormat;
threeDimensionalFormat.isVisible = true;
threeDimensionalFormat.bevelTopType = Word.BevelType.circle;
threeDimensionalFormat.bevelTopDepth = 6;
threeDimensionalFormat.bevelTopInset = 6;
threeDimensionalFormat.load("bevelTopType,bevelTopDepth,bevelTopInset");
await context.sync();
console.log("Applied bevel formatting:", {
type: threeDimensionalFormat.bevelTopType,
depth: threeDimensionalFormat.bevelTopDepth,
inset: threeDimensionalFormat.bevelTopInset,
});
});
underline
指定字体的下划线类型。
none 如果字体没有下划线。
underline: Word.UnderlineType | "Mixed" | "None" | "Hidden" | "DotLine" | "Single" | "Word" | "Double" | "Thick" | "Dotted" | "DottedHeavy" | "DashLine" | "DashLineHeavy" | "DashLineLong" | "DashLineLongHeavy" | "DotDashLine" | "DotDashLineHeavy" | "TwoDotDashLine" | "TwoDotDashLineHeavy" | "Wave" | "WaveHeavy" | "WaveDouble";
属性值
Word.UnderlineType | "Mixed" | "None" | "Hidden" | "DotLine" | "Single" | "Word" | "Double" | "Thick" | "Dotted" | "DottedHeavy" | "DashLine" | "DashLineHeavy" | "DashLineLong" | "DashLineLongHeavy" | "DotDashLine" | "DotDashLineHeavy" | "TwoDotDashLine" | "TwoDotDashLineHeavy" | "Wave" | "WaveHeavy" | "WaveDouble"
注解
示例
// Underline format text
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Create a range proxy object for the current selection.
const selection = context.document.getSelection();
// Queue a command to underline the current selection.
selection.font.underline = Word.UnderlineType.single;
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('The selection now has an underline style.');
});
underlineColor
方法详细信息
decreaseFontSize()
increaseFontSize()
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Word.Interfaces.FontLoadOptions): Word.Font;
参数
- options
- Word.Interfaces.FontLoadOptions
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Word.Font;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Word.Font;
参数
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
reset()
set(properties, options)
同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。
set(properties: Interfaces.FontUpdateData, options?: OfficeExtension.UpdateOptions): void;
参数
- properties
- Word.Interfaces.FontUpdateData
一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。
- options
- OfficeExtension.UpdateOptions
提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。
返回
void
set(properties)
基于现有加载的对象,同时在对象上设置多个属性。
set(properties: Word.Font): void;
参数
- properties
- Word.Font
返回
void
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/25-paragraph/insert-formatted-text.yaml
await Word.run(async (context) => {
// Second sentence, let's insert it as a paragraph after the previously inserted one.
const secondSentence: Word.Paragraph = context.document.body.insertParagraph(
"This is the first text with a custom style.",
"End"
);
secondSentence.font.set({
bold: false,
italic: true,
name: "Berlin Sans FB",
color: "blue",
size: 30
});
await context.sync();
});
setAsTemplateDefault()
将指定的字体格式设置为活动文档和所有基于活动模板创建的新文档的默认格式。
setAsTemplateDefault(): void;
返回
void
注解
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.Font 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.FontData) ,其中包含从原始对象加载的任何子属性的浅层副本。
toJSON(): Word.Interfaces.FontData;
返回
track()
根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。
track(): Word.Font;
返回
untrack()
释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。
untrack(): Word.Font;