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

Free Support Forum - aspose.com

insertHTML() and apply style

Good day.

I may be misunderstanding a portion of this document explaining how styles are handled when inserting HTML with Aspose. If that is the case I apologize.

Here is what we are doing. We have an existing document. In this document is defined a custom style called MyStyle. It applies to both paragraphs and characters. I have attached the document with the custom style to this post.

We load the document into aspose and use the following code to insert HTML into the document:

<cfset docBuilder.init(doc)>
<cfset docBuilder.moveToBookmark(“bookmark”)>
<cfset docBuilder.insertHtml(‘Hello there. This should be mystyle’)>
<cfset doc.save(outFile)>

When we open the output document, the inserted text does not have the style applied; it is still the “Normal” style. I have also tried applying the class to a

aragraph without success. I’ve also tried applying other non-custom styles without success.

In short, we would like to insert HTML and take advantage of styles already embedded in the document. Can you tell me if we can accomplish that? We are using Aspose.Words 11.9.0


Thank you.


Hi Chris,


Thanks for your inquiry.

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).

Moreover, Please check following C# code snippet for your kind reference.

// Create a blank document object<o:p></o:p>

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

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

// Set up and pass the object which implements the handler methods.<o:p></o:p>

doc.NodeChangingCallback = new HandleNodeChanging_FontChanger();<o:p></o:p>

// Insert sample HTML content<o:p></o:p>

builder.InsertHtml(

Hello World

”);<o:p></o:p>

doc.Save(@“C:\test\Document.FontChanger Out.doc”);<o:p></o:p>



public class HandleNodeChanging_FontChanger : INodeChangingCallback<o:p></o:p>

{<o:p></o:p>

// Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document<o:p></o:p>

void INodeChangingCallback.NodeInserted(NodeChangingArgs args)<o:p></o:p>

{<o:p></o:p>

// Change the font of inserted text contained in the Run nodes.<o:p></o:p>

if (args.Node.NodeType == NodeType.Run)<o:p></o:p>

{<o:p></o:p>

Aspose.Words.Font font = ((Run)args.Node).Font;<o:p></o:p>

font.Size = 24;<o:p></o:p>

font.Name = “Arial”;<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

void INodeChangingCallback.NodeInserting(NodeChangingArgs args)<o:p></o:p>

{<o:p></o:p>

// Do Nothing<o:p></o:p>

}<o:p></o:p>

void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)<o:p></o:p>

{<o:p></o:p>

// Do Nothing<o:p></o:p>

}<o:p></o:p>

void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)<o:p></o:p>

{<o:p></o:p>

// Do Nothing<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>


I hope, this will help.

Hi Chris,


The work around code on the following post might also be useful in this situation.

Thanks,

Thank you both. I used the workaround noted in the previous post and it performs the task well.


Hi Chris,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

The issues you have found earlier (filed as WORDSNET-1432) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(20)