Free Support Forum - aspose.com

FINDing values in a document

I know that the REPLACE functionality can also be used to FIND text. I am doing this currently. The issue I am running into is knowing, since I am not doing a replace, whether something was found or not.

I know taht within my evaluator I could set a global flag and that check that flag back in my code but this seems a little obtuse. I wanted to know if their is a better method for doing this.

Just to be sure we are on the same page, I need to find some text in the document and I simply need to know that it was found. Is their any method for doing this? Besides using public or global variables, is their some built-in functionality for returning whether the information was found, what was found or maybe even where it was found?

Thanks,

Todd

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. The Replace method returns the number of replacements made. So you can try using the following code:

Document doc = new Document(@"Test045\in.doc");

string pattern = "Test";

int result = doc.Range.Replace(new Regex(pattern), pattern);

Best regards.

I may not have made it clear enough in my initial post but I am NOT performing a replace. I don't want or need to replace anything. I just need to FIND it. My understanding was that you have no FIND functionality on it's own but that the REPLACE method performs this action. You just need to use an evaluator to stop the actual replacement from occuring.

So, I still need my initial questions answered.

Thanks,

Todd

The thing with Find is that we have not yet figured out how to expose it in the public API nicely. I will try to explain.

Aspose.Words.Document is a tree of nodes. Let's say you have the following tree:

Paragraph

Run with normal Text = "This is "
Run with bold Text = "Hello World "
Run with normal Text = " message."

Now, imagine you are trying to search for string "World message". What should the find function return? Some sort of a position? What that position should look like given that the found string starts in the middle of text of one run and then spans several runs. If you can come up with a good solution - we will implement this as Find functionality.

I understand your dilema. For a second, let me just jump back to my initial question and get a clarification that there, currently, is no easy way (besides using public variables) to pass information from the evaluator back (in the case that nothing is being replaced).

I see your problem with Find. Replace works to find the item so I would ask if there is a way to just use that same functionality but always skip the replacement aspect. Without more details I can't really see how they differ. The Replace functionality does a find (initially) but appears to avoid the Find issue you mentioned.

As I mentioned above, I might be able to assist making suggestions with find but I just don't know enough details. For example, is it possible to strip styling when performing the search? As asked above, how does the Replace method perform the find?

We can take this discussion offline to email if you want. Just let me know.

Thanks,
Todd

Looked at your first post again. If all your want is to find out if a particular string occurs in the document you can do this:

string text = doc.GetText();

this will give you all text in the document and you can do search in a string then.

Regarding using Replace functionality without replacing and figuring out if anything was found - Alexey's second post probably answers that correctly. Replace returns a number. I will just check if this is a number of occurrences made or replacements made.

Unfortunately Range.Replace returns number of replacements made, not number of occurrences encountered so cannot be used directly by you.

In addition to the getting plain text of the document as I suggested before and searching in it yourself you can use the following code to avoid using a global variable. I think this code does what you are doing now, just avoids a global variables.

internal class Finder
{
internal static bool Contains(Document doc, string text)
{
Finder finder = new Finder();
return finder.ContainsCore(doc, text);
}

private bool ContainsCore(Document doc, string text)
{
mIsFound = false;
doc.Range.Replace(new Regex(text), HandleReplace, true);
return mIsFound;
}

private ReplaceAction HandleReplace(object sender, ReplaceEvaluatorArgs e)
{
mIsFound = true;
return ReplaceAction.Stop;
}

private bool mIsFound;
}

class Program
{
static void Main(string[] args)
{
Document doc = new Document(@"C:\Users\Romeok\Desktop\DinnerInvitationDemo.doc");

Debug.WriteLine("the: " + Finder.Contains(doc, "the"));
Debug.WriteLine("xxx: " + Finder.Contains(doc, "xxx"));
}

}

I should be able to make that work for my situation.


Thanks,

Todd