We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove underline for specific symbol or word

Hi Team,

I need to remove the Underline for a particular symbol or word if it is underlined in the document. Can you please help me out with the same?

Example: If a > symbol is underlined I need to remove it.

@kkumaranil485 unfortunately there is not easy to achieve what you want, you will probably require to reorder the Runs in the paragraph to be able to apply a differential style, see the following example code:

var charToFind = '>';
Document doc = new Document("C:\\Temp\\input.docx");
Document clonDoc = (Document)doc.Clone(true);

int runIndex = -1;
int skipNewRuns = 0;

// Reorder the Runs of the Paragraph to be able to apply the desired style in the right place of the text
foreach (Run run in clonDoc.FirstSection.Body.FirstParagraph.Runs)
{
    // Skip the Runs added in the previous iteration of the loop
    if (skipNewRuns > 0)
    {
        skipNewRuns--;
        continue;
    }

    runIndex++;
    string text = run.Text;
    // Get the breaking points in the text and place it in a new Run
    var cutInedexes = text.Select((c, indx) => c == charToFind ? indx + 1 : -1).Where(i => i >= 0).ToList();
                
    if (!cutInedexes.Any()) continue;

    for (int i = 0; i < cutInedexes.Count || i == 0; i++)
    {
        Run newRun = clonDoc.ImportNode(run, true, ImportFormatMode.KeepSourceFormatting) as Run;
        newRun.Text = charToFind.ToString();

        // Remove the underline format
        newRun.Font.Underline = Underline.None;
                    
        clonDoc.FirstSection.Body.FirstParagraph.Runs.Insert(runIndex + 1, newRun);

        // If the breaking point is at the end of the text don't have text to put in a new Run  
        if (cutInedexes[i] != text.Length)
        {
            newRun = clonDoc.ImportNode(run, true, ImportFormatMode.KeepSourceFormatting) as Run;
            int startTextIndx = cutInedexes[i];
            int endTextLenght = cutInedexes.Count > 1 && i < cutInedexes.Count - 1 ? (cutInedexes[i + 1] - 1) - startTextIndx : -1;
            if(endTextLenght > 0)
            {
                newRun.Text = text.Substring(startTextIndx, endTextLenght);               
               clonDoc.FirstSection.Body.FirstParagraph.Runs.Insert(runIndex + 2, newRun);

               skipNewRuns++;
               runIndex++;
           }
           else if(endTextLenght < 0)
           {
               newRun.Text = text.Substring(startTextIndx);
                            clonDoc.FirstSection.Body.FirstParagraph.Runs.Insert(runIndex + 2, newRun);

               skipNewRuns++;
               runIndex++;
           }
       }

       skipNewRuns++;
       runIndex++;
   }

   run.Text = text.Substring(0, cutInedexes[0] - 1);
}

clonDoc.Save("C:\\Temp\\output.docx");

input.docx (13.7 KB)
output.docx (9.5 KB)