跳到主要内容

Replace

替换此范围内所有出现的指定文本。

语法​

expression.Replace(findWhat, replaceWith, matchCase, wholeWords);

expression - 表示 ApiTextRange 类的变量。

参数​

名称必需/可选数据类型默认值描述
findWhat必需string要搜索的文本。
replaceWith必需string替换文本。
matchCase可选booleanfalse区分大小写搜索。
wholeWords可选booleanfalse仅匹配整个单词。

返回值​

ApiTextRange

示例​

在 PDF 页面的形状中替换文本范围内某个单词的所有匹配项。

// The replacement keeps the formatting of the text it replaces.

// Replace every occurrence of "foo" with "bar".

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", 200 * 36000, 80 * 36000, fill, stroke);
shape.SetPosition(608400, 1267200);
page.AddObject(shape);

const docContent = shape.GetContent();
const paragraph = docContent.GetElement(0);
paragraph.AddText("foo bar foo");

const range = shape.GetTextRange();

range.Replace("foo", "bar");

const reportFill = Api.CreateSolidFill(Api.RGB(200, 230, 255));
const report = Api.CreateShape("rect", 200 * 36000, 40 * 36000, reportFill, stroke);
report.SetPosition(608400, 4000000);
page.AddObject(report);
report.GetContent().GetElement(0).AddText("Replace: " + shape.GetTextRange().GetText());