How to Highlight and Underline words

Hi,

I would like to know how to highlight and underline words in the Word Document that is displayed within the control. For example, I need to highlight the word "Hello World" in the doc file programmatically before the document itself is displayed to the user.

the keywords are dynamic and can be anything based on what the user had requested.

thanks

sample:

This is my first Hello World document. Hello World is the smalles program in the world

Easy as it seems this task requires quite a bit of coding with our current API. This will be imp[roved in the future however.

I will compose a sample code and post it here tomorrow.

Best regards,

Here is the a complete example. In fact there are two examples in one. One is for the text stated in your message and the second is a more complex test of search and format functionality. The code looks a bit coomplicated. That is due to an XML like document model of Aspose.Words. We are going to simplify the range selection functionality in the future versions.

Here is the code:

private void SearchAndFormatExample1()

{

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("This is my first Hello World document. Hello World is the smallest program in the world.");

doc.Range.Replace(new Regex("Hello World"), new ReplaceEvaluator(SearchAndFormatEvaluator), false);

string resultFilename = Application.StartupPath + @"\testSearchAndFormat1 Out.doc";

// Save document to file

doc.Save(resultFilename);

// Open document in MS Word to check results.

System.Diagnostics.Process.Start(resultFilename);

}

private void SearchAndFormatExample2()

{

Document doc = new Document(Application.StartupPath + @"\testSearchAndFormat.doc");

doc.Range.Replace(new Regex("fox"), new ReplaceEvaluator(SearchAndFormatEvaluator), false);

string resultFilename = Application.StartupPath + @"\testSearchAndFormat2 Out.doc";

// Save document to file

doc.Save(resultFilename);

// Open document in MS Word to check results.

System.Diagnostics.Process.Start(resultFilename);

}

// Define the desired formatting here.

private void ApplyFormatting(Run run)

{

run.Font.Color = System.Drawing.Color.Blue;

run.Font.Bold = true;

}

private ReplaceAction SearchAndFormatEvaluator(object sender, ReplaceEvaluatorArgs e)

{

Run run = (Run)e.MatchNode;

Paragraph para = run.ParentParagraph;

// We are using /xbf (inverted question mark) symbol for temporary purposes.

// Any symbol will do that is not special and is guaranteed not to be present in the document.

matchedRun = run;

para.Range.Replace(new Regex(e.Match.Value), new ReplaceEvaluator(RunReplaceEvaluator), false);

Run subrun = (Run)run.Clone(false);

int pos = run.Text.IndexOf("\xbf");

subrun.Text = subrun.Text.Substring(0, pos);

run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1);

para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subrun);

Run selectionRun = new Run(run.Document, e.Match.Value);

para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), selectionRun);

ApplyFormatting(selectionRun);

// Let's remove run if it is empty.

if (run.Text == "")

run.Remove();

// No replace action is necessary - we have already done what we intended to do.

return ReplaceAction.Skip;

}

private Run matchedRun;

private ReplaceAction RunReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)

{

if ((Run)e.MatchNode == matchedRun)

{

e.Replacement = "\xbf";

matchedRun = null;

return ReplaceAction.Replace;

}

else

return ReplaceAction.Skip;

}

Best regards,

Hi,

C# is a foreign language to me :) , and so I have translated the code snippet to VB.net.

In case there are any other VB'er's who are put off by the curly bracket, I have included the code below (please bear in mind that although this code works for me, it may still contain bugs). It might be useful if there are any Aspose people who want to look over it, and confirm/reject it!

' Define the desired formatting here.

Private Sub ApplyFormatting(ByVal Run As Run)

Run.Font.Color = System.Drawing.Color.Aqua

Run.Font.Bold = True

End Sub

Private matchedRun As Run

Private Function SearchAndFormatEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction

Dim run As Run = e.MatchNode

Dim para As Paragraph = run.ParentParagraph

' We are using ASCII Character 168 (inverted question mark) symbol for temporary purposes.

' Any symbol will do that is not special and is guaranteed not to be present in the document.

matchedRun = run

para.Range.Replace(New Regex(e.Match.Value), New ReplaceEvaluator(AddressOf RunReplaceEvaluator), False)

Dim subrun As Run = run.Clone(False)

Dim pos As Integer = run.Text.IndexOf(Chr(168)) '("\xbf")

subrun.Text = subrun.Text.Substring(0, pos)

run.Text = run.Text.Substring(pos + 1, run.Text.Length - pos - 1)

para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), subrun)

Dim selectionRun As New Run(run.Document, e.Match.Value)

para.ChildNodes.Insert(para.ChildNodes.IndexOf(run), selectionRun)

ApplyFormatting(selectionRun)

' Let's remove run if it is empty.

If run.Text = "" Then

run.Remove()

End If

' No replace action is necessary - we have already done what we intended to do.

Return ReplaceAction.Skip

End Function

Private Function RunReplaceEvaluator(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction

If e.MatchNode Is matchedRun Then

e.Replacement = Chr(168) '"\xbf"

matchedRun = Nothing

Return ReplaceAction.Replace

Else

Return ReplaceAction.Skip

End If

End Function

Thank you for translation! I am sure it will be very useful for VB .NET folks.