Extracting and inserting HTML into a SDT

I have a problem that could be caused by how I am doing something, so I am looking for advice on the best way to achieve it.

I have a RichText SDT which contains other SDTs (dropdown list) and PlainText. If I save as HTML, and then make some changes and then try and reimport that Html I lose all SDTs, is that expected behavior? The following code example

So in this example, I am extracting a SDT which contains other SDTs, and when I try and insert that HTML back into the SDT i lose all formatting. How can I replace the html of an SDT?

Document tempDoc = new Document();
Node dstSdt = tempDoc.ImportNode(sdt, true, ImportFormatMode.KeepSourceFormatting);
tempDoc.FirstSection.Body.AppendChild(dstSdt);
var htmlstring = "";
using (MemoryStream htmlStream = new MemoryStream())
{
    tempDoc.Save(htmlStream, SaveFormat.Html);
    htmlstring = Encoding.UTF8.GetString(htmlStream.GetBuffer(), 0, (int)htmlStream.Length);
}

DocumentBuilder builder = new DocumentBuilder(target);
builder.MoveTo(sdt);
builder.InsertHtml(htmlstring);

@markchequer Please note, Aspose.Words is designed to work with MS Word documents. HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another.
Aspose.Words partially preserves SDT in HTML using -aw-sdt-XXX attributes. But due to limitations of HTML format is is not always possible to preserve all MS Word document features after DOCX->HTML->DOCX roundtrip.

Understand. Is there any way I can format or create an SDT using InsertHtml or does it just not create the SDTs?

@markchequer As I have mentioned, Aspose.Words partially preserves SDT in HTML using -aw-sdt-XXX attributes. For example the following HTML recreates SDT in the document:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml("<div style=\"-aw-sdt-tag:'sdt_tag'; -aw-sdt-title:'MySdt'\"><p style=\"margin-top:0pt; margin-bottom:8pt\"><span>Structured document tag</span></p></div>");
doc.Save(@"C:\Temp\out.docx");

Thank you, I was not sure if InsertHtml would even support it, so this is great. Thank you.

1 Like