Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Nov 06, 2017 9:23 am

Hello all

I have a service that generates an html file and then i convert that to word. Within the logic of generating the html some words need to be highlighted. The only way i see how to do that is when i generate the <span> tag i add a background style for the highlight. so i get something like this:
<span style="backgroundcolor:yellow"> my generated text </span>
This works fine and the resulting word file has the text highlighted.
My problem is this: the only way to get the highlight off when editing the word file (which is necessary) is to select the highlight text and then "remove styling". I cant just remove the highlight because its embedded in the style of the span tag. This confuses my users. Is there any other way to highlight the text? I cant use the way the documentation shows because how they do it the document is already there and you can select text and highlight it. I have to do it via the HTML that i generate.
Hope i explained my issue well enough. any ideas?
Thanks
Roy

shakazulu89
 
Posts: 4
Joined: Sun Nov 27, 2016 11:05 am

Mon Nov 06, 2017 10:04 am

Hello,

Thanks for your inquiry.
In regards to your issue, you could first load the HTML file you generated as a Document, and then add and remove the highlight effect. Please see the below code.
Code: Select all
 Spire.Doc.Document document = new Spire.Doc.Document();
            document.LoadFromFile(@"sample.htm", FileFormat.Html, XHTMLValidationType.None);
            TextSelection[] text = document.FindAllString("my generated text", false, true);
            //add highlight to the text
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }
            //remove the highlight effect
            foreach (TextSelection seletion in text)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.White;
            }

            document.SaveToFile("FindHighlight.docx", FileFormat.Docx);


If the issue still bothers you, just feel free to write back and share a sample file.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Nov 07, 2017 8:55 am

Hi,

Greetings from e-iceblue!
How is your issue now? Your feedback will be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Nov 10, 2017 12:27 pm

Thanks for the reply

The issue is once i have the html ready its not possible for me to know what text needs to be highlighted (as it is now in the code). I guess ill have to do some rewriting to get this to work.

Thank you

shakazulu89
 
Posts: 4
Joined: Sun Nov 27, 2016 11:05 am

Mon Nov 13, 2017 4:01 am

Hello shakazulu89,

Thanks for your response.
If you want to add the highlight effect, sorry you have to first know the text range object that you want to add to. Considering your situation, there's another approach. If you only want to get the highlight off, you could traverse all the text range object in the document, and remove the highlight effect.
Code: Select all
 private void test12062()
        {
            Spire.Doc.Document document = new Spire.Doc.Document();
            document.LoadFromFile(@"sample.htm", FileFormat.Html, XHTMLValidationType.None);
            foreach (Section s in document.Sections)
            {
                if(s.HeadersFooters.Header !=null)
                {
                    RemoveHighlight(s.HeadersFooters.Header);
                }

                if (s.HeadersFooters.Footer != null)
                {
                    RemoveHighlight(s.HeadersFooters.Footer);
                }

                RemoveHighlight(s.Body);
                 
            }
            document.SaveToFile("FindHighlight.docx", FileFormat.Docx);
        }

        private static void RemoveHighlight(DocumentObject obj)
        {
            foreach (DocumentObject fObj in obj.ChildObjects)
            {
                if (fObj is Paragraph)
                {
                    Paragraph para = fObj as Paragraph;
                   
                    foreach (DocumentObject pObj in para.ChildObjects)
                    {
                        if (pObj is TextRange)
                        {
                            TextRange tr = pObj as TextRange;
                            if (tr.CharacterFormat.HighlightColor == Color.Yellow)
                            {
                                tr.CharacterFormat.HighlightColor = Color.Transparent;
                            }
                        }
                    }
                }
            }
        }


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Nov 14, 2017 8:38 am

Hi shakazulu89,

Greetings from E-iceblue!
How is your issue now? Your feedback will be of great help to us.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc