Finding a pattern in the exact sequence

I need to find all occurrences of a pattern in a document. It’s important that the list of matches is organized according to their appearance in the document. However, the find and replace function always provides results from the header and footer at the end. Is there a possibility for this that I have missed?

@kvaak You can perform find/replace operation on a separate nodes in your document. If special order is required you can do it by traversing nodes in your document in the proper order. For example:

Document doc = new Document(@"C:\Temp\in.docx");

foreach (Section s in doc.Sections)
{
    // Find replace in section headers
    foreach (HeaderFooter hf in s.HeadersFooters)
    {
        if (hf.IsHeader)
        {
            // Find replace in the sections's headers
            //....
            hf.Range.Replace("pattern", "replacement");
        }
    }

    // Find replace in the section's body
    //....
    s.Body.Range.Replace("pattern", "replacement");

    // And do the same for footers
    // Find replace in section footers
    foreach (HeaderFooter hf in s.HeadersFooters)
    {
        if (!hf.IsHeader)
        {
            // Find replace in the sections's footers
            //....
            hf.Range.Replace("pattern", "replacement");
        }
    }
}

doc.Save(@"C:\Temp\out.docx");
1 Like

Thank you for the response. A manual search would be very time-consuming and inelegant. I would have expected the exact sequence to be the default. However, I have now found another way to solve this elegantly. When using LegacyMode = true, the desired order is maintained. Should there be concerns that this mode will be removed without replacement in future versions?

@kvaak No, we do not have plans to remove LegacyMode property. It has been added when we changed find/replace operation order to match MS Word. The property has been added not to harm users who relies on the old behavior.

1 Like

That sounds great. Thank you very much, and at this point, I would like to commend the very prompt and excellent support.

1 Like

I just noticed that the example doesn’t seem to work. Even if I have a document with 2 pages, I still have only one section, although I have two pages. Reading in the exact order is not possible like this.

@kvaak There is no page concept in MS Word documents, since they are flow documents by their nature. Consumer applications layout the document into pages on the fly. Section on MS Word document can contain any content and can ocuppy multiple pages. Please see our documentation to learn about Document Object model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

I am already familiar with this page. I have reviewed it again now, and I am still unable to find a solution. Isn’t it intended to display the matches for a pattern sorted by their occurrence in the document?

@kvaak Aspose.Words mimics MS Word behavior and matches the occurrences in the same order as MS Word does.