How to find font and color has been used for the string

How to find font and color has been used for the string in c#
and
How to find font and color has been used for the string in whole word document

@siva1950 You can use Font properties to get font name, color and other formatting properties.
Default font properties applied to the document are accessible via StyleCollection.DefaultFont property.
Also, please see the following article to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

Hi Alexey
Thank you for your information.But I was stucked some scenario
I have sharing sample code below and explain from there kindly help me

var replaceValue = objDocStructurePlanning.ValidationSummary(j, objEntitylst, t.DocType, mlTexts, true);
FindReplaceOptions optionsFrm = new FindReplaceOptions(FindReplaceDirection.Backward);
optionsFrm.ReplacingCallback = new ReplaceWithHtmlEvaluator(replaceValue.ToString());
mainDoc.Range.Replace(new Regex(@"<Validation Plan>"), String.Empty, optionsFrm);

In my document I have <Validation Plan> Tag .after some logic I have replace <Validation Plan> tag .replace code also I have shared below

private class ReplaceWithHtmlEvaluator : IReplacingCallback
{
    internal ReplaceWithHtmlEvaluator(string html)
    {
        _html = html;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Aspose.Words.Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Aspose.Words.Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder((Aspose.Words.Document)e.MatchNode.Document);
        builder.MoveTo((Aspose.Words.Run)runs[0]);

        // Replace '<CustomerName>' text with a red bold name.
        //builder.InsertHtml(_html);
        foreach (Aspose.Words.Run run in runs)
            run.Remove();

        var parts = Regex.Split(_html, @"(<h1>[\s\S]+?<\/h1>)|(<h2>[\s\S]+?<\/h2>)|(<h3>[\s\S]+?<\/h3>)").Where(l => l != string.Empty).ToArray();

        foreach (var splitHtml in parts)
        {
            InsertNewPageWithHeading(builder, splitHtml);
        }

        return ReplaceAction.Skip;
    }
}

My Expectation is <Validation Tag> Font details I need to get.Kindly Help on this scenario

@siva1950 If you need to get font details of the matched node you can use code like this:

// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;

// Get the matched node font details
Font currentFont = ((Run)currentNode).Font;
// Here you can get font name and other formatting properties of the matched node.
Console.WriteLine(currentFont.Name);
Console.WriteLine(currentFont.Bold);
Console.WriteLine(currentFont.Color);
// etc....

Great ,Thank you So much what I have Expected I got it now. Appreciating more
Last Question
Before Request Which I have shared code .In that code I have been Using few line at End

foreach (var splitHtml in parts)
{
    InsertNewPageWithHeading(builder, splitHtml);
}

this InsertNewPageWithHeading Method I have been Mainly using for Generating Toc Let me explain I have been sharing code for this InsertNewPageWithHeading Method

public static void InsertNewPageWithHeading(DocumentBuilder builder, string captionText)
{
    try
    {

        if (captionText.Contains("<h1>"))
        {
            string styleName = "Heading 1";
            string originalStyle = builder.ParagraphFormat.StyleName;
            builder.ParagraphFormat.Style = builder.Document.Styles[styleName];
            builder.ParagraphFormat.Style = builder.Document.Styles[styleName];
            string Heading1 = captionText.Replace("<h1><a name=_Toc424545941>", "").Replace("</a></h1>", "").Replace("<h1><a name='_Toc418583172'>", "")
                .Replace("</a></h1>", "").Replace("<h1><a name='_Toc418583177'>", "").Replace("<h1><a name='_Toc418583188'>", "").Replace("<h1><a name='_Toc418583164'>", "").Replace("</a></h1>", "")
                .Replace("<h1><a name='_Toc418583169'>", "").Replace("<h1><a name='_Toc418583189'>", "").Replace("</a></h1>", "");
            //builder.Font.Name = "Times New Roman";
            //builder.Font.Bold = true;
            //builder.Font.Size = 14;
            builder.Font.Name = Fontname;
            builder.Font.Bold = FontBold;
            builder.Font.Italic = FontItalic;
            builder.Font.Size = Fontsize;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.Write(Heading1);
            builder.InsertBreak(BreakType.ParagraphBreak);

            return;
        }
        else if (captionText.Contains("<h2>"))
        {
            string styleName = "Heading 2";
            string originalStyle = builder.ParagraphFormat.StyleName;
            builder.ParagraphFormat.Style = builder.Document.Styles[styleName];
            string Heading2 = captionText.Replace("<h2><a name='_Toc418583172'>", "").Replace("</a></h2>", "").Replace("<h2><a name=_Toc424545948><strong><em>", "")
                .Replace("</em></strong></a><strong><em></em></strong></h2>", "").Replace("<h2><a name=_Toc424545969><strong><em>", "")
                .Replace("</em></strong></a><strong><em> </em></strong></h2>", "").Replace("<h2><a name=_Toc424545968><strong><em>", "")
                .Replace("</em></strong></a><p><strong><em> </em></strong></p></h2>", "").Replace("<h2><a name=_Toc424545969><strong><em>", "")
                .Replace("</em></strong></a><strong><em> </em></strong></h2>", "").Replace("<h2><a name='_Toc418583171'>", "").Replace("<h2><a name='_Toc418583189'>", "")
                .Replace("</em></strong></a><p><strong><em></em></strong></p></h2>", "");
            //builder.Font.Name = "Times New Roman";
            //builder.Font.Bold = true;
            //builder.Font.Size = 12;
            builder.Font.Name = Fontname;
            builder.Font.Bold = FontBold;
            builder.Font.Italic = FontItalic;
            builder.Font.Size = Fontsize;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.Writeln(Heading2.Trim());
            return;
        }
        else if (captionText.Contains("<h3>"))
        {
            string styleName = "Heading 3";
            string originalStyle = builder.ParagraphFormat.StyleName;
            builder.ParagraphFormat.Style = builder.Document.Styles[styleName];
            string Heading3 = captionText.Replace("<h3><a name='_Toc418583171'>", "").Replace("</a></h3>", "").Replace("<h3><a name=_Toc424545948><strong><em>", "").Replace("</em></strong></a><strong><em></em></strong></h3>", "")
                .Replace("<h3><a name='_Toc418583176'><strong><em>", "").Replace("</em></strong>", "").Replace("<h3><a name=_Toc42454554><strong><em>", "")
                .Replace("<h3><a name=_Toc424545875><strong><em>", "").Replace("<h3><a name=_Toc42454554><strong><em>", "");
            //builder.Font.Name = "Times New Roman";
            //builder.Font.Bold = true;
            //builder.Font.Size = 12;
            builder.Font.Name = Fontname;
            builder.Font.Bold = FontBold;
            builder.Font.Italic = FontItalic;
            builder.Font.Size = Fontsize;
            builder.Writeln(Heading3.Trim());
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

            return;
        }
        // builder.InsertHtml(captionText, false);
        builder.InsertHtml(captionText, false);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

From this Method I am Finding Header H1,H2,H3 Tag and passing to Documentbuilder For Generating TOC.

Normally I have been using follow Aspose Generating TOC and it Working Fine shared below code

builder.InsertTableOfContents("\\o \"1-4\" \\h \\z \\u");
wdMainDoc.Save(saveFilename);

but while Replacing Tag value Scenario the TOC is not Generated so I have followed this my custom Method “InsertNewPageWithHeading” and representing H1,H2,H3 for document builder
After Creating this InsertNewPageWithHeading I can able to generate the TOC.
Here Iam using Extra Method to avoid this method "InsertNewPageWithHeading " any other way is there or Whether Iam doing Incorrect way?

@siva1950 HTML h1-h6 tags are converted to the corresponding heading paragraphs when HTML is imported by Aspose.Words. So there is no need of additional processing. For example see the following code:

// Create document and DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert TOC.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Inert page break (for demonstraction urposes)
builder.InsertBreak(BreakType.PageBreak);
// Insert HTML with headings:
builder.InsertHtml("<h1>This is heading 1</h1><h2>This is heading 2</h2><h3>This is heading 3</h3>");
// Update fields.
doc.UpdateFields();
// Save output document
doc.Save(@"C:\Temp\out.docx");

thanks,
Hi alexey
How to find wherther the document contains toc or not?

Kindly provide in c# code

@siva1950, please check this post: How to know the document contains TOC or not - #3 by carlos.molina

public static int GetDocumentSettings(System.IO.Stream fileStream = null, string fileName = "", Boolean reviewReq = true)
{
    var returnValue = 0;

    try
    {
        if (fileStream != null)
        {
            if (Path.GetExtension(fileName).ToLower() == ".doc" || Path.GetExtension(fileName).ToLower() == ".docx")
            {
                using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument wordDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(fileStream, true))
                {

                    var settings = wordDoc.MainDocumentPart.DocumentSettingsPart.Settings;
                    var removedocumentProtection = settings.Elements<DocumentFormat.OpenXml.Wordprocessing.DocumentProtection>().FirstOrDefault();
                    if (removedocumentProtection != null)
                    {
                        // Remove the DocumentProtection element
                        removedocumentProtection.Remove();
                        wordDoc.MainDocumentPart.DocumentSettingsPart.Settings.Save();
                    }
                    var trackRevisions = settings.Elements<DocumentFormat.OpenXml.Wordprocessing.TrackRevisions>().FirstOrDefault();
                    var documentProtection = settings.Elements<DocumentFormat.OpenXml.Wordprocessing.DocumentProtection>().Where(x => x.Enforcement == true);
                    if (documentProtection != null && documentProtection.Count() > 0)
                    {
                        returnValue = 1; // Document Protected with or without Password
                    }
                    else if (trackRevisions != null && trackRevisions.Val != "false" && returnValue == 0 && reviewReq)
                    {
                        returnValue = 2; // Track Change Enabled
                    }

                }
            }
        }
        return 0;
    }
    catch (Exception ex)
    {
        throw ex;

    }
}

I have been try remove ms word document protection through aspose but I dont have any idea because I am using filestream.through filestream how to remove can you suggest code in c# more helpful for me

@siva1950 You can use Document.Unprotect method to remove protection of the document.