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

Free Support Forum - aspose.com

Inconsistance behaviour

hi,

I am using aspose.words for my application. And I am getting some difficulty in my application, as due to some special characters my code functionality changes.
like I am using same functionality for the words (form-, -form, form_, *form). Now after the processing (change the font color and replace in the document). Now what I am getting is that the words (form-. -form) are comes as i want but the words (form, form) font color is unchanged.

input document contains
form-, -form, form*, *form

output document contains
form-, -form, form*, *form

while what I want is the output document should contains
form-, -form, form*, *form

I am using a replace evaluator form which the evaluator is called for the first two words while for next two words not calling the evaluator. Below is a part of code which i am using…

If wordToSave.StartsWith("-") Then
wordToSave = wordToSave.Replace("-", String.Empty)
End If
If wordToSave.EndsWith("-") Then
wordToSave = wordToSave.Replace("-", String.Empty)
End If
If wordToSave.StartsWith("*") Then
wordToSave = wordToSave.Replace("*", String.Empty)
End If
If wordToSave.EndsWith("*") Then
wordToSave = wordToSave.Replace("_", String.Empty)
End If

Dim s As String = RegularExpressions.Regex.Escape(WordToColor)

Dim rex As RegularExpressions.Regex
rex = New RegularExpressions.Regex("\b(?<![A-Za-z0-9][-]+)(?<!’)(?<!’)" & s & "(?![-]+[A-Za-z0-9])\b", RegularExpressions.RegexOptions.IgnoreCase)

For Each section As Section In oDoc
section.Body.Range.Replace(rex, New ReplaceEvaluator(AddressOf SearchAndFormatEvaluator), False)
Next

please help me out…
Thanks…!

Hi
Thanks for your request. Please attach your document for testing and provide me code of SearchAndFormatEvaluator you are using in your program. I will investigate the problem and provide you a solution.
Best regards.

I tried to debug and analyse the problem again and found that the SearchAndFormatEvaluator is called in each case but the problem was within this function. Please check this. There may be bacause i am using another replace evaluator.
I have attached the code file and the document which i use for testing.

Hi
Thanks for your request. The problem occurs because your Regex does not match all “form” words in your document. Please try using the following code:

Sub Main()
Dim doc As Document = New Document("C:\Temp\test.doc")
Dim s As String = RegularExpressions.Regex.Escape("form")
Dim rex As RegularExpressions.Regex
rex = New RegularExpressions.Regex(s, RegularExpressions.RegexOptions.IgnoreCase)
For Each section As Section In doc.Sections
section.Body.Range.Replace(rex, New ReplaceEvaluator(AddressOf SearchAndFormatEvaluator), False)
Next
doc.Save("C:\Temp\out.doc")
End Sub
Dim matchedRun As Run
Private Function SearchAndFormatEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction
'Get MatchNode
Dim run1 As Run = CType(e.MatchNode, Run)
'Create Run
Dim run2 As Run = CType(run1.Clone(True), Run)
'Get index of match value
Dim index As Integer = run1.Text.LastIndexOf(e.Match.Value)
'split run that contains matched text 
run2.Text = run1.Text.Substring(index + e.Match.Value.Length)
run1.Text = run1.Text.Substring(0, index)
run1.ParentParagraph.InsertAfter(run2, run1)
'Create document builder
Dim builder As DocumentBuilder = New DocumentBuilder(e.MatchNode.Document)
'Move to run2
builder.MoveTo(run2)
'Change font
builder.Font.Color = Color.Blue
'Insert value
builder.Write(e.Match.Value)
Return ReplaceAction.Skip
End Function

Hope this helps.
Best regards.

I have tried the same code as you suggested in the post and with the attached document I am getting an exception message as “Length cannot be less than zero. Parameter name: length”.

And for the regular expression I am using, I have such type of regex as i want to add some extra functionality.
Like
As the used blue font color in the earlier post was just for test, in real functionality the font color of different words may be different according to the words category.

i.e. In the attached document the word “post-form” comes in a category whose font color should be blue, while in the same document the “form” should be in red color, post similarly the other words may be of different font colors.

So, if i used the regex as

rex = New RegularExpressions.Regex(s, RegularExpressions.RegexOptions.IgnoreCase)

it seems to overwrite the font color of the word “form” as “post-form” while it should be like “post-form”.

Thats why to ignore such words i modified the regex as

rex = New RegularExpressions.Regex("\b(?<![A-Za-z0-9][-]+)(?<!’)(?<!’)" & s & "(?![-]+[A-Za-z0-9])\b", RegularExpressions.RegexOptions.IgnoreCase)

And for my formatting function I am using Run object as parameter. so can you suggest how to use it. I tried to use it but it results in the wrong placeing of words in output document.

is still i am going in wrong direction?

Thanks,
Prafull Gupta

Hi
Thanks for your request. There are still problems with your Regex. It does not match all necessary cases. So please try to modify you Regex so it works for all needed cases.
Best regards.