Inserting an HTML into a TextBox shape

Received : 2007/07/25 10:41:50
Message : I want to insert an HTML text into a TextBox shape having Aspose.word rendering the HTML structure and building the text with the right formatting into that TextBox shape.
Unfortunately, the InsertHtml method works only for DocumentBuilder object - I could not find such for a regular shape (so I didn't find an easy way to insert HTML into shape), however, I tried building a dummy DocumentBuilder object and by looking at the results of the DocumentBuilder.InsertHtml method I could see that the HTML content is translated into multilpe run objects under doc.FirstSection.Body.FirstParagraph.Runs[...] - well, this is nice, I though of copying the run objects and inserting them into a paragraph in my TextBox shape, in that way I use a dummy DocumentBuilder that will do the rendering of HTML for me and than I'll copy the resulted run objects, right?
Unfortunately I could not find a way to copy the run objects. Regular assignment does not work since they have created based on different documents, Clone does not work either (trying to clone the whole paragraph) - So I end up looking for help,
>> What is the easiest way to insert an Html text into a TextBox shape that will draw it with the correct formatting?

Thanks.


This message was posted using Aspose.Live 2 Forum

Hi,

You do not need to copy runs in this case. Just move the document builder's cursor into a textbox and insert your HTML:

Document doc = new Document();

Shape shape = new Shape(doc, ShapeType.TextBox);

Paragraph paragraph = new Paragraph(doc);

Run run = new Run(doc);

paragraph.AppendChild(run);

shape.AppendChild(paragraph);

doc.FirstSection.Body.FirstParagraph.AppendChild(shape);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveTo(shape.FirstParagraph.FirstChild);

builder.InsertHtml("Bold");

Hope this helps.

This work if I want the whole textbox to be HTML based.
Now, assume that I already inserted something into the textbox and want to add the HTML data into the shape, is there a way to mark the location before the insertion (during the insertion of previous text) so I will be able to moveto into the exact location without knowing whether it is the first child or not? (I assume having possible multiple runs before)

Thanks.

No problem at all. Just do

builder.MoveTo(shape.LastParagraph.LastChild);

instead of moving to the first child of the first paragraph.