Surround a node (or few of them) by html tags

Dear Sir or Madam,

I build the Aspose tree by inserting the nodes into it. Some nodes (or sometimes a group of nodes) has to be surrounded by custom html tags. In attached example I’d like to have open/closing tags around paragraph + table, around paragraph, around table, around the cell in the table and around the second run in the paragraph. I attached the html file I’d like to have, please look for . I understand that there is InsertHtml method in the DocumentBuilder and I can use MoveTo to inser opening tag in front of a node, but is there an easy way to insert closing tags after the node? Thank you in advance,
AlexAspose.zip (1.1 KB)

@AlexanderNovickov You can wrap node into custom tags using InsertBefore/InsertAfter methods, but I am afraid such tags will not be exported to HTML as tags, they will be exported as text content, which will make them not usable.
If you need these tags in the resulting HTML, the output HTML document postprocessing will be required.

Hi Alexey, yes, thank you for suggestion, it was my plan B solution. One more question if you don’t mind. I changed the code to move around the document using the DocumentBuilder and insert HTML using InsertHtml method. I would expect the paragraph being surrounded by custom tags and the run as well, but it just doesn’t work. What am I doing wrong?
thank you in advance,
AlexProgram.zip (617 Bytes)

@AlexanderNovickov InsertHtml interprets the inserted snippet. To insert custom tag as text you should use Write method:

Document doc = new Document();
DocumentBuilder db = new DocumentBuilder(doc);
Paragraph para = new Paragraph(doc);
doc.LastSection.Body.AppendChild(para);
db.MoveTo(para);
db.Write("<para-start>");
Run r0 = new Run(doc, "First run");
para.AppendChild(r0);
Run r1 = new Run(doc, "Secondrun run");
para.AppendChild(r1);
db.MoveTo(r1);
db.Write("<run-start>");
db.MoveToDocumentEnd();
db.Write("</run-end>");
Run r2 = new Run(doc, "Last run");
para.AppendChild(r2);
db.MoveToDocumentEnd();
db.Write("</para-end>");
Table t = new Table(doc);
doc.LastSection.Body.AppendChild(t);
Row row = new Row(doc);
t.AppendChild(row);
Cell c = new Cell(doc);
row.AppendChild(c);
Paragraph p = new Paragraph(doc);
c.AppendChild(p);
Run cellrun = new Run(doc, "Test");
p.AppendChild(cellrun);
doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.html", new HtmlSaveOptions() { PrettyFormat = true });

Thank you, Alexey, really appreciated. If I use Write is there way to save curly brackets unescaped, rather than as &gt; and &lt;?
Also the example puts the “para” tags inside the span, is there a way to put them before/after

tag?

<para-start>
    <p style="margin-top:0pt; margin-bottom:0pt">
        <span>First run&lt;run-start&gt;Secondrun run&lt;/run-end&gt;Last run</span>
    </p></para-end>

rather than

<p style="margin-top:0pt; margin-bottom:0pt">
    <span>&lt;para-start&gt;First run&lt;run-start&gt;Secondrun run&lt;/run-end&gt;Last run&lt;/para-end&gt;</span>
</p>

@AlexanderNovickov Yes, this exactly what I mean that inserted tags will be interpreted as regular text and will be escaped while exporting to HTML. So the only way to insert custom tags into HTML is postprocessing HTML produced by Aspose.Words.

Thank you Alexey!

1 Like