Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jun 22, 2022 8:55 pm

Hi,

Word provides ability to create nested content controls. Is there a way to add a content control inside another content control using Spire.Doc?
I have tried the following to embed a Picture Content Control inside a Rich Text Control, but it doesn't seem to work.

Code: Select all
            Section stagingSection = doc.AddSection();
            Paragraph stagingParagraph = stagingSection.AddParagraph();
            stagingParagraph.AppendHTML(htmlContent);

            StructureDocumentTag rtsdt = CreateDocumentControl(alias, tag, SdtType.RichText, LockSettingsType.UnLocked);
            Section richTextControlSection = doc.AddSection();
            richTextControlSection.Body.ChildObjects.Add(rtsdt);
            StructureDocumentTag sdt = CreateDocumentControl(alias, tag, SdtType.Picture, LockSettingsType.SDTLocked);
            Section pictureControlSection = doc.AddSection();
            pictureControlSection.Body.ChildObjects.Add(sdt);
            //DocPicture pic = new DocPicture(doc) { Width = 10, Height = 10 };
            //DocPicture pic = paragraph.AppendPicture(htmlContent);
            //pic.LoadImage(Image.FromFile("Logo.jpg"));
            //pic.LoadImage(htmlContent);
            foreach (DocumentObject obj in stagingSection.Body.ChildObjects)
            {
                //Clone the elements from section in sdt
                sdt.SDTContent.ChildObjects.Add(obj.Clone());
            }
            //Remove the html section
            doc.Sections.Remove(stagingSection);
           
           
           
            foreach (DocumentObject obj in pictureControlSection.Body.ChildObjects)
            {//obj is a StructureDocumentTag
                //Clone the elements from section in sdt
                rtsdt.SDTContent.ChildObjects.Add(obj.Clone());
            }
            doc.Sections.Remove(pictureControlSection);



jalpaashara
 
Posts: 21
Joined: Wed Jun 22, 2022 8:12 pm

Thu Jun 23, 2022 6:40 am

Hello,

Thanks for your inquiry.

I tested your code but the content controls are nested(show in the screenshot).
A6F6DC9B-1D3F-4aef-8C41-19AE900307B6.png


I've also tried adding some extra content to the outer content control, and they're still nested.
BAE18986-5524-45d7-9C9F-06A871ABFC60.png

Clicking the content control:
559596B4-2B14-45b7-AFD9-CC407C28BB03.png


Here is the modified code:
Code: Select all
            Document doc = new Document();

            Section stagingSection = doc.AddSection();
            Paragraph stagingParagraph = stagingSection.AddParagraph();
            stagingParagraph.AppendHTML("<html><h1>this is Document </h1></html>");

 
            StructureDocumentTag rtsdt = new StructureDocumentTag(doc);

            Section richTextControlSection = doc.AddSection();
            richTextControlSection.Body.ChildObjects.Add(rtsdt);
            rtsdt.SDTProperties.SDTType = SdtType.RichText;
            rtsdt.SDTProperties.LockSettings = LockSettingsType.UnLocked;
            rtsdt.SDTProperties.Alias = "alias-richtext";
            rtsdt.SDTProperties.Tag = "tag-richtext";

            StructureDocumentTag sdt = new StructureDocumentTag(doc);
            Section pictureControlSection = doc.AddSection();
            pictureControlSection.Body.ChildObjects.Add(sdt);

            sdt.SDTProperties.SDTType = SdtType.Picture;
            sdt.SDTProperties.LockSettings = LockSettingsType.SDTLocked;
            sdt.SDTProperties.Alias = "alias-picture";
            sdt.SDTProperties.Tag = "tag-picture";

            //DocPicture pic = new DocPicture(doc) { Width = 10, Height = 10 };
            //DocPicture pic = paragraph.AppendPicture(htmlContent);
            //pic.LoadImage(Image.FromFile("Logo.jpg"));
            //pic.LoadImage(htmlContent);
            foreach (DocumentObject obj in stagingSection.Body.ChildObjects)
            {
                //Clone the elements from section in sdt
                sdt.SDTContent.ChildObjects.Add(obj.Clone());
            }
            //Remove the html section
            doc.Sections.Remove(stagingSection);

            Paragraph paragraph = new Paragraph(doc);
            paragraph.AppendText("Hello");
            rtsdt.SDTContent.ChildObjects.Add(paragraph);
            foreach (DocumentObject obj in pictureControlSection.Body.ChildObjects)
            {//obj is a StructureDocumentTag
                //Clone the elements from section in sdt
                rtsdt.SDTContent.ChildObjects.Add(obj.Clone());
            }
            doc.Sections.Remove(pictureControlSection);

            doc.SaveToFile("output.docx",FileFormat.Docx);


Could you please provide us with your full test code and the HTML strings used in order to reproduce the issue you encountered? You can upload them here or send them to us via email([email protected]). Thanks in advance.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Thu Jun 23, 2022 7:26 pm

It is working for me as well. I was adding the same title for outer and inner content control without any paragraph before the inner content control which ended up confusing me as in word it looks like they were stacked up and I wasn't able to see the outer content control. Apologies for the confusion. Appreciate your response. Thank You!

jalpaashara
 
Posts: 21
Joined: Wed Jun 22, 2022 8:12 pm

Fri Jun 24, 2022 3:16 am

You're welcome.
I wish you all the best.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc