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
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set up and pass the object which implements the handler methods.
doc.NodeChangingCallback = new HandleNodeChanging_FontChanger();
// Insert sample HTML content
builder.InsertHtml("<b>Hello World</b>");
doc.Save(@"C:\test\Document.FontChanger Out.doc");
public class HandleNodeChanging_FontChanger : INodeChangingCallback
{
    // 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.Size = 24;
            font.Name = "Arial";
        }
    }
    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }
}

I hope, this will help.

Hi Chris,

The work around code on thefollowing 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)