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

Free Support Forum - aspose.com

Merge HTML into existing paragraph

Using a custom IFieldMergingCallback class, I’m merging HTML into a word document. This all works pretty much as expected. However, I’ve run into one scenario that I’m not sure how to implement. If my word document contains the following line:

Section Title: {{fieldname}}

The mailmerge procedure correctly inserts the html into the document. However, if the HTML starts off with a paragraph (e.g.

field html contents here

), then the inserted HTML appears on the next line as shown here:
Section Title:
field html contents here

During the mailmerge process, is there a way to determine if the html resulted in a new paragraph and if so, merge the new paragraph with the previous paragraph?

Thanks in advance.

Hi,


Thanks for your inquiry. You can implement INodeChangingCallback interface if you want to receive notifications when nodes are inserted or removed in the document. You can get an idea from the following example which shows how to implement custom logic over node insertion in the document by changing the font of inserted HTML content.

public void TestNodeChangingInDocument()

{

// 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("

Hello World

");

doc.Save(MyDir + "Document.FontChanger Out.doc");

// Check that the inserted content has the correct formatting

Run run = (Run)doc.GetChild(NodeType.Run, 0, true);

Assert.AreEqual(24.0, run.Font.Size);

Assert.AreEqual("Arial", run.Font.Name);

}

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

Best regards,

Thanks, the INodeChangingCallback was the key to what I needed.