Highlight Text

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

Hi Sreedhar,

Thanks for your inquiry. Please use the same approach shared here to find and highlight the text. I have tested the scenario using following code example and have not found any issue. I have attached the output document with this post for your kind reference.

Document doc = new Document(MyDir + "InputDocument.html");
Regex regex = new Regex(@"Dextromethorphan HBr \(INN\)/Dextromethorphan HBr \(USAN\) ", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new ReplaceEvaluatorFindAndHighlight(), true);
regex = new Regex(@"1/100 and < 1/10", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new ReplaceEvaluatorFindAndHighlight(), true);
regex = new Regex(@"For children 2 to 6 years, the maximum total daily recommended dose of dextromethorphan is 30 mg over a 24-hour period taken in divided doses not greater than 7.5 mg per dose", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new ReplaceEvaluatorFindAndHighlight(), true);
doc.Save(MyDir + "Out.html");

Hi,
It is not working on the following text.
Post-marketing Data:
Adverse drug reactions (ADRs) identified during post-marketing experience with Dextromethorphan are included in (CCDS) Table 2 and 3. In these tables, the frequencies are provided according to the following convention:
Highlight the above text with the same scenario.
Thanks
Sreedhar Dharanikota

Hi Sreedhar,

Thanks for your inquiry. In your case, there are two test cases 1) text is in multiple lines or paragraphs 2) text is simple and in single line of paragraph. For first case, please use the code example shared in following forum link.
https://forum.aspose.com/t/47638

For second case, please use the same approach shared here to find and highlight the text. For this case, you may also use the code shared in this forum link. However, there are some extra steps in this code example.

Hope this answers your query. Please let us know if you have any more queries.