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);