We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Highlight text from RichText

Hi,

I have problem to highlight html text provided from a RichText. I don’t how can I “convert” css class to word style. I tried using Word heading. It works, but I have useless line feed before and after the highlighted text.

This is a example with this string:
This a

test with

highlight

This is the style for H4 in my RichText page
H4 {
background-color: “color”;
text-decoration:none;
font-size: 8pt;
margin: 1;
font-weight:normal;
display:inline;
}

This is my code when I create the Word document
private static void CreateHighlightStyle(ref Document doc, Color color)
{
bool hightlightStyleExists = doc.Styles.Cast().Any(s => s.Name == “Heading 4”);
if (!hightlightStyleExists)
{
Style style = doc.Styles.Add(StyleType.Paragraph, “Heading 4”);
style.Font.Underline = Underline.None;
style.Font.Shading.BackgroundPatternColor = color;
}
}

To insert text in document, I’m using builder.InsertHtml:
Font font = builder.Font;
string textToInsert = “This a

test with

highlight”;
builder.Document.NodeChangingCallback = new HandleNodeFont(font);
builder.InsertHtml(textToInsert);
builder.Document.NodeChangingCallback = null;

I let you 3 attachments:
- The RichText
- What I have in the result document
- What I should have

I have to use style to not lose the trace of highlighted text because the user can change the color without generate the document (only reapply styles with the selected color).

I don’t know if it’s the best way to do this or if I have to change all!

Can you help me to solve this problem (I hope I’m quite understandable!)??

Thank you and have a nice day!!!

Steeve

Hi Steeve,


Thanks for your inquiry. The tags

to

are imported into the Aspose.Words DOM as the built-in Heading styles: Heading 1 - Heading 6. Similarly, Paragraphs with built-in heading styles are exported as

-

elements.

In your case, I suggest you please use the Span tag instead of H4. Hope this helps you. Please let us konw if you have any more queries.

Document doc = new
Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml(File.ReadAllText(MyDir + "in.html"));

doc.Save(MyDir + "out.docx");

<html>

<head>

<style type="text/css">

.highlighted {

background-color:yellow;

text-decoration:none;

font-size: 12pt;

font-name: arial;

margin: 1;

font-weight:normal;

display:inline;

}

</style>

</head>

<body>

<body>

This a <span class=highlighted>test with</span> highlight

</body>

</html>


Hi,

Thank you for the suggestion. I tried it, but it does not work. The highlight color is not applied.

I have made a small prototype of my application (see attachment). The final document in it is only a part of my final document, but I think that it explain what I would like.

The document “Test.docx” is my result when I’m running the prototype.

Do not forget that all strings come from a RichText that support italic/bold/bullets/etc.

I’m sure that I do something incorrect, but I do not know what!!

Thank you for your help and have a nice day!

Steeve

Hi Steeve,


Thanks for sharing the detail.

Please note that content inserted by Insert HTML does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content, e.g. if font is not specified in your HTML snippet, default font will be applied (Times New Roman).

You are inserting style (highlighted) in InsertStyleHtmlHighlight method and separatly insrting the html with Span tag.

const string mandate = “To design, supply, install, and commission a new
SCADA/DMSSS, which operates and
maintains the transmission …”
;
<o:p></o:p>


Please use the DocumentBuilder.InsertHtml method as shown in following code snippet.

Document doc = new
Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml(@"

.highlighted {

background-color:" + "yellow" + @";

text-decoration:none;

font-size: 15pt;

font-family:Arial;

margin: 1;

font-weight:normal;

display:inline;

}

To design, supply, install, and commission a new SCADA/DMSSS, which operates and maintains the transmission ...

");

doc.Save(MyDir + "out.docx");



Moreover, you can achieve your requirement by using following code snippet. Hope this helps you.


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

InsertStyleHtmlHighlight(builder, Color.Yellow);

builder.Writeln("To design, supply, install, and commission a new ");

// Set the node changing handler to catch inserted nodes and pass the node range object used to the store

// the nodes are looking for.

doc.NodeChangingCallback = new HandleNodeChanging_FontChanger(doc.Styles["highlighted"]);

// Insert sample HTML content

builder.InsertHtml("SCADA/DMSSS");

doc.NodeChangingCallback = null;

builder.Writeln(", which operates and maintains the transmission ...");

doc.Save(MyDir + "out.docx");

public class HandleNodeChanging_FontChanger : INodeChangingCallback

{

Style style = null;

public HandleNodeChanging_FontChanger(Style newStyle)

{

style = newStyle;

}

// Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document

void INodeChangingCallback.NodeInserted(NodeChangingArgs args)

{

// Change the font of inserted text contained in the Run nodes.

if (args.Node.NodeType == NodeType.Run)

{

Aspose.Words.Font font = ((Run)args.Node).Font;

font.Style = style;

}

}

void INodeChangingCallback.NodeInserting(NodeChangingArgs args)

{

// Do Nothing

}

void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)

{

// Do Nothing

}

void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)

{

// Do Nothing

}

}

private static void InsertStyleHtmlHighlight(DocumentBuilder builder, Color color)

{

builder.InsertHtml(@"

.highlighted {

background-color:" + "yellow" + @";

text-decoration:none;

font-size: 15pt;

font-family:Arial;

margin: 1;

font-weight:normal;

display:inline;

}

");

}


Hi,

Your suggestion works, but only with simple html text. In my case, I have other html tag (bullet / italics / bold) to manage. For example, if the span tag is in another html tag (bullets), the html is broken and the result is not correct. I’m looking for a manner to manage the class and other html tags.

I have adjusted my prototype to show you what is my result with the following string:
"Supply included all 

  • hardware
  • software
  • documentation
  • training 
  • as well as the supply and installation of eleven substation
  • Other Test
  • Last Test
"

I let you my prototype and the result of my test in attachment.

I need to assign a style to span tag to be able to change the highlight color without regenerate the document:
// Document doc comes from disk
public static void ApplyNewColorOnStyle(ref Document doc)
{

Style highlighted = doc.Styles[“highlighted”];
highlighted.Font.Shading.BackgroundPatternColor = Color.Green; // Color selected by the user

}

Thank you for your help and have a nice day!

Steeve

Hi Steeve,


Thanks for your inquiry. In your case, I suggest you please use the code shared at following documentation link to find and highlight the text instead of using highlighted style.
http://www.aspose.com/docs/display/wordsnet/How+to+Find+and+Highlight+Text

Please replace your html (highlighted style) as shown in following and use the same approach shared at above link to highlight the text.

Replace :
supply and installation
To :
[[supply and installation]]

Please read following forum links for your kind reference. Hope this helps you.
https://forum.aspose.com/t/52256
https://forum.aspose.com/t/52645

If the problem still remains, please attach your Word document after replace span tag with [[supply …]] here for testing. I will investigate the issue on my side and provide you more information along with code.