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

Free Support Forum - aspose.com

Trouble inserting HTML to a StructuredDocumentTag?

We have an existing SDT workflow we’d like to use Aspose to improve, if possible.

I’m having difficulty inserting HTML into a SDT tag.

I’ve attached an entire free-standing demo project, including a sample .docx file, but the key code that’s failing goes like this:

/// 
/// Set a simple SDT text field using plain text
/// 
private static void SetSdtHtmlText(Document doc, StructuredDocumentTag sdtToChange, string htmlForSdt)
{
    // Check.EnsureNotNull(doc);
    // Check.EnsureNotNull(sdtToChange);

    // Reset IsShowingPlaceholderText to hide placeholder text.
    sdtToChange.IsShowingPlaceholderText = false;

    // Null fields are just blank
    htmlForSdt = htmlForSdt ?? string.Empty;

    var tmpParagraph = new Paragraph(doc);
    var tmpRun = new Run(doc);
    tmpParagraph.AppendChild(tmpRun);

    sdtToChange.RemoveAllChildren();
    // DOES NOT WORK
    sdtToChange.AppendChild(tmpParagraph);

    // We need to use Document Builder to insert HTML.
    var tmpDocBuilder = new DocumentBuilder(doc);
    tmpDocBuilder.MoveTo(tmpParagraph);

    tmpDocBuilder.InsertHtml(htmlForSdt);
}

This gives me a “Cannot insert a node of this type at this location.” error.

There are a other posts in this area, but none of them have helped me figure out what to do here.

All help appreciated

Hi Stewart,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behavior as MS Word do. If you try to insert a Paragraph inside content control (ProjectsCoverageAssessment), MS Word also does not allow to insert a Paragraph. This is the reason you are facing the issue. Please read the detail of StructuredDocumentTag from here:
https://reference.aspose.com/words/net/aspose.words.markup/structureddocumenttag

var tmpParagraph = new Paragraph(doc);
var tmpRun = new Run(doc);
tmpParagraph.AppendChild(tmpRun);
sdtToChange.RemoveAllChildren();
// DOES NOT WORK
sdtToChange.AppendChild(tmpParagraph);