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

Free Support Forum - aspose.com

Re: How to insert HTML contend in a Run or Table.Cell

Hi,
I have the same problem, i want add some html string to a Table.Cell. I should be using objects not the documentbuilder.
Can you give me a example, how i can do it? (in C#)
I have html strings in the DB saved. Example:

Hello


Hi Alain,

Thanks for your inquiry. In your case, we suggest you please load the HTML string into Aspose.Words.Document and insert it into your desired location using following InsertDocument method. Hope this helps you.

Document docHtml = null;

String html = @"

Hello

";

// Convert Html string to byte array.

byte[] htmlBytes = Encoding.UTF8.GetBytes(html);

Aspose.Words.LoadOptions loadoptions = new Aspose.Words.LoadOptions();

loadoptions.LoadFormat = LoadFormat.Html;

// Create stream.

using (MemoryStream rtfStream = new MemoryStream(htmlBytes))

{

// Open document from stream.

docHtml = new Document(rtfStream, loadoptions);

}

Document doc = new Document(MyDir + "in.docx");

InsertDocument(doc.FirstSection.Body.Tables[0].FirstRow.FirstCell.FirstParagraph, docHtml);

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

///

/// Inserts content of the external document after the specified node.

/// Section breaks and section formatting of the inserted document are ignored.

///

/// insertAfterNode">Node in the destination document after which the content

/// should be inserted. This node should be a block level node (paragraph or table).

/// srcDoc">The document to insert.

static void InsertDocument(Node insertAfterNode, Document srcDoc)

{

// Make sure that the node is either a paragraph or table.

if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &

(!insertAfterNode.NodeType.Equals(NodeType.Table)))

throw new ArgumentException("The destination node should be either a paragraph or table.");

// We will be inserting into the parent of the destination paragraph.

CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.

NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);

// Loop through all sections in the source document.

foreach (Section srcSection in srcDoc.Sections)

{

// Loop through all block level nodes (paragraphs and tables) in the body of the section.

foreach (Node srcNode in srcSection.Body)

{

// Let's skip the node if it is a last empty paragraph in a section.

if (srcNode.NodeType.Equals(NodeType.Paragraph))

{

Paragraph para = (Paragraph)srcNode;

if (para.IsEndOfSection && !para.HasChildNodes)

continue;

}

// This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = importer.ImportNode(srcNode, true);

// Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

}