- Code: Select all
private async Task ReplaceContentControlText(Spire.Doc.Document document, string tag, string textToReplace, string newText)
{
Body body = document.Sections[0].Body;
// Create lists for paragraphs and tables
List<Paragraph> paragraphs = new List<Paragraph>();
for (int i = 0; i < body.ChildObjects.Count; i++)
{
// Get the document object
DocumentObject documentObject = body.ChildObjects[i];
// If it is a StructureDocumentTag object
if (documentObject.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
StructureDocumentTag structureDocumentTag = (StructureDocumentTag)documentObject;
if (structureDocumentTag.SDTProperties.Tag == tag || structureDocumentTag.SDTProperties.Alias == tag)
{
for (int j = 0; j < structureDocumentTag.ChildObjects.Count; j++)
{
// If it is a paragraph object
if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Paragraph)
{
Paragraph paragraph = (Paragraph)structureDocumentTag.ChildObjects[j];
//paragraphs.Add(paragraph);
paragraph.Replace(textToReplace, newText ?? string.Empty, true, true);
//paragraph.Text = newText;
document.AcceptChanges();
}
}
}
}
}
// Modify the text content of the first paragraph
if (paragraphs.Count > 0)
{
Console.WriteLine(paragraphs[0].Text);
paragraphs[0].Replace(textToReplace, newText ?? string.Empty, true, true);
//paragraphs[0].Text = newText;
Console.WriteLine(paragraphs[0].Text);
}
}