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 Feb 08, 2023 7:25 pm

Hi,
Is there a way convert TextRange to Sdt?
Thanks,
Andrei

andrei.chorin
 
Posts: 48
Joined: Sun Jan 08, 2023 2:36 pm

Thu Feb 09, 2023 9:10 am

Hi,

Thanks for your inquiry.
Please see the following code for reference.
Code: Select all
        // get its owner paragraph
        Paragraph paragraph = textRange.getOwnerParagraph();
        // get the index of the textRange
        int originIndex = paragraph.getChildObjects().indexOf(textRange);
        // create a sdt instance
        StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
        sd.getSDTProperties().setSDTType(SdtType.Text);
        SdtText text = new SdtText(true);
        text.isMultiline(true);
        sd.getSDTProperties().setControlProperties(text);
        TextRange rt = new TextRange(document);
        rt.setText("text");
        sd.getSDTContent().getChildObjects().add(rt);

        // insert the sdt
        paragraph.getChildObjects().insert(originIndex,sd);
        // remove the original textRange
        paragraph.getChildObjects().removeAt(originIndex + 1);


If the code does not meet your requirement, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc