Hi,
As per our conversation i am sending my html input document and desired output document.
i am using dynamic content to highlight the text i.e. data from database.
Text may contain special characters and tables and breaks anything must be highlighted.
finally binding the text to div(Html Control).
NOTE:- I am not using word document to highlight text.
Please follow the same approach and try to highlight the text.
Below is the code
var GetCCDSSectionDetail = objBusinessObjects.GetCCDSSectionContainForPreview(CCDSDevlopmentID).ToList();
if (GetCCDSSectionDetail.Count > 0)
{
StringBuilder secContent = new StringBuilder();
foreach (var sectionitem in GetCCDSSectionDetail)
{
int SectionID = Convert.ToInt32(sectionitem.SectionID);
int CommentCount = Convert.ToInt32(sectionitem.CommentCount);
var DeviationContent = objBusinessObjects.tbl_Deviations.Where(c => c.CCDSID == CCDSDevlopmentID && c.CCDSSectionID == SectionID).Select(c => c.CCDSelectedText).ToList();
if (CommentCount != 0)
{
sbSectionContent.Append("<b>" + sectionitem.SectionName.ToString() + "</b><a href=’’ onclick='return funGetSectionDetail(" + SectionID + "," + CCDSDevlopmentID + ")’; class=‘diviationcount’> Review ( " + CommentCount + " )");
}
else
{
sbSectionContent.Append("<b>" + sectionitem.SectionName.ToString() + "</b>");
}
// Code to Highlight Text
if (DeviationContent.Count() > 0)
{
string tessst = string.Empty;
StringBuilder strbuilder = new StringBuilder();
String strDeviation = string.Empty;
MemoryStream stream = new MemoryStream();
Regex regx;
DocumentBuilder builder;
string body = "";
if (DeviationContent.Count() == 1)
{
foreach (var item in DeviationContent)
{
string strSectionContent = sectionitem.SectionContent.ToString();
byte[] bytes = new byte[strSectionContent.Length * sizeof(char)];
System.Buffer.BlockCopy(strSectionContent.ToCharArray(), 0, bytes, 0, bytes.Length);
MemoryStream newStream = new MemoryStream(bytes);
Aspose.Words.Document doc = new Aspose.Words.Document(newStream);
doc.Range.Replace(ControlChar.NonBreakingSpaceChar + "", " ", false, false);
strDeviation = DeviationContent[0];
builder = new DocumentBuilder(doc);
regx = new Regex(strDeviation.Trim(), RegexOptions.IgnoreCase);
doc.Range.Replace(regx, new ReplaceEvaluatorFindAndHighlight(), false);
doc.Save(stream, SaveFormat.Html);
body = System.Text.Encoding.UTF8.GetString(stream.ToArray());
strbuilder.Append(body);
}
sbSectionContent.Append("" + strbuilder.ToString() + "
");
}
else
{
string strSectionContent = sectionitem.SectionContent.ToString();
byte[] bytes = new byte[strSectionContent.Length * sizeof(char)];
System.Buffer.BlockCopy(strSectionContent.ToCharArray(), 0, bytes, 0, bytes.Length);
MemoryStream newStream = new MemoryStream(bytes);
Aspose.Words.Document doc = new Aspose.Words.Document(newStream);
builder = new DocumentBuilder(doc);
doc.Range.Replace(ControlChar.NonBreakingSpaceChar + "", " ", false, false);
for (int i = 0; i <= DeviationContent.Count() - 1; i++)
{
strDeviation = DeviationContent[i];
regx = new Regex(strDeviation.Trim(), RegexOptions.IgnoreCase);
doc.Range.Replace(regx, new ReplaceEvaluatorFindAndHighlight(), false);
doc.Save(stream, SaveFormat.Html);
body = System.Text.Encoding.UTF8.GetString(stream.ToArray());
body += body;
}
strbuilder.Append(body);
sbSectionContent.Append("" + strbuilder.ToString() + "");
}
}
else
{
sbSectionContent.Append("" + sectionitem.SectionContent + "");
}
}
divSectionDetails.InnerHtml = System.Web.HttpUtility.HtmlDecode(sbSectionContent.ToString());
}
Thanks
Sreedhar Dharanikota