Find and Replace using style in aspose

Does Aspose word provides functionality to find all the elements of specific style?
MS Word provides the functionality for finding the lement by style.
If Aspose provides the same please share the code snippet?

@mhtsharma9,

Thanks for your inquiry. You can get the style of paragraph using ParagraphFormat.StyleName property. In your case, we suggest you please iterate over paragraph nodes, get its style, and match it with your desired style name. You can remove the desired paragraph using Paragraph.Remove method and insert your content.

If you still face problem, please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query along with code.

I need all the elements matching with a specific Character Style not paragraph.
Then how can I iterate over all the elements matching with a particular character style.

@mhtsharma9,

Thanks for your inquiry. Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style.
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles).
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

You can use Run.Font.Style to get or set the character style applied to this formatting. Following code example shows how to use style identifier to find text formatted with a specific character style and apply different character style.

Document doc = new Document(MyDir + "in.docx");

// Select all run nodes in the document.
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);

// Loop through every run node.
foreach (Run run in runs)
{
    // If the character style of the run is what we want, do what we need. Change the style in this case.
    // Note that using StyleIdentifier we can identify a built-in style regardless 
    // of the language of Microsoft Word used to create the document.
    if (run.Font.StyleIdentifier.Equals(StyleIdentifier.Emphasis))
        run.Font.StyleIdentifier = StyleIdentifier.Strong;
}

doc.Save(MyDir + "output.docx");