跳到主要内容

SetFontFamily

使用指定的字体系列设置所有 4 个字体槽。

语法​

expression.SetFontFamily(fontFamily);

expression - 表示 ApiParagraph 类的变量。

参数​

名称必需/可选数据类型默认值描述
fontFamily可选string | null用于当前段落的一个或多个字体系列。传递 null 或省略此参数可移除字体系列。

返回值​

ApiParagraph | null | undefined

示例​

设置 PDF 中段落的字体类型。

// How do I change the font family in a paragraph in a PDF?

// Apply a specific typeface to a paragraph in a PDF.

const doc = Api.GetDocument();
const page = doc.GetPage(0);

const fill = Api.CreateSolidFill(Api.RGB(255, 111, 61));
const stroke = Api.CreateStroke(0, Api.CreateNoFill());
const shape = Api.CreateShape('rect', Api.MillimetersToEmus(300), Api.MillimetersToEmus(130), fill, stroke);
shape.SetPosition(Api.MillimetersToEmus(20), Api.MillimetersToEmus(35));
const docContent = shape.GetContent();
const paragraph = docContent.GetElement(0);
paragraph.AddText("This is a paragraph with the font family set to 'Consolas'.");
paragraph.SetFontFamily('Consolas');
page.AddObject(shape);