Reputation: 1
I am trying to change font of a specific word to Italic from word document using Spire.Doc. But code is not giving me the expected result.
Exaample - Albia A et al [l0] and Jain M et al [3] also observed predominance of non-neoplastic lesions with few cases of adenocarcinoma. Incidence of chronic cholecystitis was also comparable to Shah H et al [11], Terada T [12] and Zahrani IH et al [13] who encountered 82.25%, 94.1% and 96.08% cases respectively.
So, from above paragraph only 'et al' should be formatted to Italic, everything else should stay as it is.
Pasting down the code that I have tried below.
Body body = document.Body;
var paragraphs = body.Descendants<Paragraph>();
foreach (Paragraph para in paragraphs)
{
if (para.InnerText.Contains("et al"))
{
var runs = para.Descendants<Run>();
if (runs != null)
{
foreach (Run run in runs)
{
Index index = para.InnerText.IndexOf("et al");
para.Parent.ElementAt(index).AppendChild(new Italic());
}
}
}
}
I am new to using Spire.Doc. Kindly suggest.
Upvotes: 0
Views: 850
Reputation: 1003
You can refer to the following code sample to change the font style of a specific word in a Word document:
using Spire.Doc;
using Spire.Doc.Documents;
namespace ChangeFontStyle
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Input.docx");
//Search for a specific word in the document
TextSelection[] selection = doc.FindAllString("et al", false, true);
//Change font style for all occurrences of the word
foreach (TextSelection text in selection)
{
text.GetAsOneRange().CharacterFormat.Italic = true;
}
//Save the result document
doc.SaveToFile("ChangeFontStyle.docx", FileFormat.Docx2013);
doc.Close();
}
}
}
Upvotes: 1