Ok, here is what you need to do. In HandleMergeField replace the current else block with the folllowing code:
KeyValuePair<string, bool>[] phrases = _spellChecker.SplitPhraseOnSpellCheck((string)e.FieldValue);
DocumentBuilder builder = new DocumentBuilder(e.Document);
// Move builder to merge field. The merge field itself is removed after movement is done.
builder.MoveToMergeField(e.FieldName);
// Save default formatting for merged text.
Underline defaultUnderline = builder.Font.Underline;
for (int i = phrases.Length - 1; i >= 0; i--)
{
KeyValuePair<string, bool> phrase = phrases[i];
bool phraseCorrect = phrase.Value;
string phraseText = phrase.Key;
// Apply highlighting formatting if necessary.
if (!phraseCorrect)
builder.Font.Underline = Underline.Wavy;
builder.Write(phraseText);
// Restore default formatting.
builder.Font.Underline = defaultUnderline;
}
Please note that to make the above code work correctly, SplitPhraseOnSpellCheck method should preserve all text including spaces and punctuation marks. Please make sure that it does because, I am actually inserting the parts produced by this method one by one in the above code. If you omit something when parsing - it will be missing from the document text too.
Here, I can suggest the following method to split the text into parts (words, spaces and punctuation marks):
string text = e.FieldValue.ToString();
ArrayList textParts = new ArrayList();
foreach(Match match in textParser.Matches(text))
{
textParts.Add(match.Value);
}
It breaks text into words, spaces and punctuatuion marks and uses the following regular expression for parsing:
private static Regex textParser = new Regex(@"\w+|\W", RegexOptions.Compiled);
I think you will be better off using this method as you would be able to underline separate words for incorrect spelling, just as MS Word does it.
Hope this helps,