SetText
用指定文本替换当前文本块的全部文本,同时保留文本块属性。
语法
expression.SetText(text);
expression - 表示 ApiRun 类(文本块)的变量。
参数
| 名称 | 必需/可选 | 数据类型 | 默认值 | 描述 |
|---|---|---|---|---|
| text | 必需 | string | number[] | 文本块的新文本,可以是字符串或字符代码数组。 |
返回值
示例
在 PDF 中通过一次调用替换文本运行的全部文本。
// How do I replace the text of a run without losing its formatting in a PDF?
// Swap the wording of a run on the page while its formatting stays in place 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("flowChartMagneticTape", 150 * 36000, 65 * 36000, fill, stroke);
shape.SetPosition(608400, 1267200);
const docContent = shape.GetContent();
const paragraph = docContent.GetElement(0);
const run = Api.CreateRun();
run.SetBold(true);
run.SetFontSize(30);
run.AddText("Draft wording.");
paragraph.AddElement(run);
run.SetText("Final wording, still bold.");
page.AddObject(shape);