Can't create Placeholder text for Content Control

I’m programmatically creating a content control and want to put my own text in the Placeholder. I’ve tried numerous things, including first removing the default text, but I cannot get it to display as the faint text prompt and instead it just shows as regular text…

public static void TestCreateContentControl(string ccTitle, string ccTag, string ccPlaceholder, bool ssPreventEditing, bool ssPreventDeletion, string ssFilenameAndPath)
{

        Document doc = new Document(ssFilenameAndPath);
        Paragraph para = new Paragraph(doc);
        StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);

        if (ssPreventEditing == true)
        {
            // Prevent users from editing the contents of the Content Control
            sdtRichText.LockContents = true;
        }
        else
        {
            // Allows users to edit the contents of the Content Control
            sdtRichText.LockContents = false;
        }

        if (ssPreventDeletion == true)
        {
            // Prevent users from deleting the Content Control
            sdtRichText.LockContentControl = true;
        }
        else
        {
            // Allows users to delete the Content Control
            sdtRichText.LockContentControl = false;
        }

        sdtRichText.Tag = ccTag;
        sdtRichText.Title = ccTitle;
        sdtRichText.ChildNodes.Add(para);
        doc.FirstSection.Body.AppendChild(sdtRichText);

        //// Remove default Placeholder (i.e. text prompt)
        //Paragraph target = (Paragraph)sdtRichText.LastChild;
        //sdtRichText.RemoveAllChildren();

        // Set the text value
        Run run = new Run(doc);
        run.Text = ccPlaceholder;
        run.Font.Name = "Arial";
        run.Font.Size = 10;
        para.Runs.Add(run);

        //GlossaryDocument glossaryDoc = doc.GlossaryDocument;
        //Aspose.Words.BuildingBlocks.BuildingBlock substituteBlock = new Aspose.Words.BuildingBlocks.BuildingBlock(glossaryDoc);
        //substituteBlock.Name = "MyTestPlaceholder";
        //substituteBlock.AppendChild(new Section(glossaryDoc));
        //substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
        //substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
        //glossaryDoc.AppendChild(substituteBlock);

        // Convert the text value to a Placeholder
        sdtRichText.IsShowingPlaceholderText = true;

        doc.Save(ssFilenameAndPath);

    }

@SCDGLC

Could you please ZIP and attach your input, problematic output and expected output documents? We will investigate the issue and provide you more information on it.

I’m calling it using this…

TestCreateContentControl(“TestTitle”, “TestTag”, “TestPlaceholder”,true,true, @“c:\temp\CreateCC.docx”);

And I’ve attached the actual output and the desired output.

Thanks

CreateCC.zip (25.7 KB)

@SCDGLC

Please call StructuredDocumentTag.RemoveAllChildren method as shown below to get the desired output.

StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdtRichText.RemoveAllChildren();

I already tried that (as can be seen from the commented out code) but it enters the text as regular text and not Placeholder text.

@SCDGLC

You can use the following code example to get the desired output. Hope this helps you.

Document doc = new Document();
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);

sdtRichText.Tag = "tag";
sdtRichText.Title = "title";

Paragraph para = (Paragraph)sdtRichText.GetChild(NodeType.Paragraph, 0, true);
para.Runs[0].Text = "New text";

doc.FirstSection.Body.AppendChild(sdtRichText);

doc.Save(MyDir + "output.docx");

Ah perfect, thank you!!