Setting Default Text in RichText Content Control

I am working on generating a form with content prefilled in. However I have noticed, that when I am supplying the text, the behavior when the user opens the form and clicks in to make further edits is to highlight all of the text and not place the cursor in the form field.

This does not appear to happen if i create an empty field, then fill it out and reopen the word document.

If you can provide me with any assistance to have the form behave as it would had the form been edited in word initially and opened, that would be appreciated.

Below is the code I’ve used, one generates an empty field. One generates a field with the default text above it and some empty text, and the final where I’ve emptied the fields children then prefilled the text.

var filePath = @"C:\Users\Default\Documents\sample_form.docx";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
var sdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt.Tag = "empty";
doc.FirstSection.Body.AppendChild(sdt);

var sdt2 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt2.Tag = "filled";
Paragraph para = new Paragraph(doc);
Run run = new Run(doc);
run.Text = "Hello World";
para.Runs.Add(run);
//sdt2.RemoveAllChildren();
sdt2.GetChildNodes(NodeType.Any, false).Add(para);
doc.FirstSection.Body.AppendChild(sdt2);

var sdt3 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt3.Tag = "cleaned";
Paragraph para2 = new Paragraph(doc);
Run run2 = new Run(doc);
run2.Text = "Hello World";
para2.Runs.Add(run2);
sdt3.RemoveAllChildren();
sdt3.GetChildNodes(NodeType.Any, false).Add(para2);
doc.FirstSection.Body.AppendChild(sdt3);

doc.Save(filePath);

@slberry75 To prevent highlighting whole SDT content when it is clicked in MS Word, it is required to set StructuredDocumentTag.IsShowingPlaceholderText property to false.
For example:

StructuredDocumentTag sdt3 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt3.Tag = "cleaned";
Paragraph para2 = new Paragraph(doc);
Run run2 = new Run(doc);
run2.Text = "Hello World";
para2.Runs.Add(run2);
sdt3.RemoveAllChildren();
sdt3.GetChildNodes(NodeType.Any, false).Add(para2);
sdt3.IsShowingPlaceholderText = false;

Thank you Alexey. i should have asked earlier. My google foo is weak, but I found it in the documentation after reading your answer and will bookmark that section.

I do have a follow-up question. I tried reading the structured document tag to note any difference, and didn’t see one. Is there a cast I could have used to see that this property was not set on the sample document I created in word?

I am new to Aspose and welcome any debugging tips to stumble on these answers myself.

@slberry75 When StructuredDocumentTag node is created from scratch, it’s IsShowingPlaceholderText is set to true by default. To stop treating the SDT content as a placeholder text it is require to set this property to false, as I have shown in the previous code example.