PdfContentEditor.ReplaceText

ReplaceText(string, int, string, TextState)

替换指定页面上的 PDF 文件中的文本。可以指定 TextState 对象(字体系列,颜色)来替换文本。

public bool ReplaceText(string srcString, int thePage, string destString, TextState textState)
参数类型描述
srcStringString要替换的字符串。
thePageInt32页码(0 表示“所有页面”)。
destStringString替换后的字符串。
textStateTextState文本状态(文本颜色,字体等)。

返回值

如果进行了替换,则返回 true。

示例

该示例演示如何在 PDF 文档的第一页上替换文本,并为新文本设置 TextState 文本属性。

// open document
Document doc = new Document(inFile);

// Create font and mark it to be embedded
Aspose.Pdf.Text.Font font = FontRepository.FindFont("Courier New");
font.IsEmbedded = true;

// create PdfContentEditor object to edit text
PdfContentEditor editor = new PdfContentEditor();
editor.BindPdf(doc);

// create textState object
TextState textState = new TextState();
textState.Font = font;
textState.FontSize = 17;
textState.FontStyle = FontStyle.Bold | FontStyle.Italic;
textState.ForegroundColor = Color.Red;

// change text with specified font
editor.ReplaceText("hello world", 1, "hi world", textState);

// save document
doc.Save(outFile);

另请参阅


ReplaceText(string, string)

替换 PDF 文件中的文本。

public bool ReplaceText(string srcString, string destString)
参数类型描述
srcStringString要替换的字符串。
destStringString替换字符串。

返回值

如果进行了替换,则返回 true。

示例

该示例演示如何在 PDF 文档中替换文本。

// open document
Document doc = new Document(inFile);

// create PdfContentEditor object to edit text
PdfContentEditor editor = new PdfContentEditor();
editor.BindPdf(doc);

// change text 
editor.ReplaceText("hello world", "hi world");

// save document
doc.Save(outFile);

另请参阅


ReplaceText(string, int, string)

替换指定页面上的 PDF 文件中的文本。

public bool ReplaceText(string srcString, int thePage, string destString)
参数类型描述
srcStringString要替换的字符串。
thePageInt32页码(0 表示所有页面)
destStringString替换字符串。

返回值

如果进行了替换,则返回 true。

示例

该示例演示如何在指定页面上替换 PDF 文档中的文本。

// open document
Document doc = new Document(inFile);

// create PdfContentEditor object to edit text
PdfContentEditor editor = new PdfContentEditor();
editor.BindPdf(doc);

// change text 
editor.ReplaceText("hello world", 1, "hi world");

// save document
doc.Save(outFile);

另请参阅


ReplaceText(string, string, TextState)

使用指定的 TextState 对象替换 PDF 文件中的文本。

public bool ReplaceText(string srcString, string destString, TextState textState)
参数类型描述
srcStringString要替换的字符串
destStringString替换字符串
textStateTextState文本状态(文本颜色,字体等)

返回值

如果进行了替换,则返回 true。

示例

该示例演示如何替换文本并为新文本设置 TextState 文本属性。

// open document
Document doc = new Document(inFile);

// Create font and mark it to be embedded
Aspose.Pdf.Text.Font font = FontRepository.FindFont("Courier New");
font.IsEmbedded = true;

// create PdfContentEditor object to edit text
PdfContentEditor editor = new PdfContentEditor();
editor.BindPdf(doc);

// create textState object
TextState textState = new TextState();
textState.Font = font;
textState.FontStyle = FontStyle.Bold | FontStyle.Italic;

// change text with specified font
editor.ReplaceText("hello world", "hi world", textState);

// save document
doc.Save(outFile);

另请参阅


ReplaceText(string, string, int)

替换 PDF 文件中的文本并设置字体大小。

public bool ReplaceText(string srcString, string destString, int fontSize)
参数类型描述
srcStringString要替换的字符串。
destStringString替换字符串。
fontSizeInt32字体大小。

返回值

如果进行了替换,则返回 true。

示例

该示例演示如何替换文本并为新文本设置字体大小。

// open document
Document doc = new Document(inFile);

// Create font and mark it to be embedded
Aspose.Pdf.Text.Font font = FontRepository.FindFont("Courier New");
font.IsEmbedded = true;

// create PdfContentEditor object to edit text
PdfContentEditor editor = new PdfContentEditor();
editor.BindPdf(doc);

// change text with specified font
editor.ReplaceText("hello world", "hi world", 14);

// save document
doc.Save(outFile);

另请参阅