locked
How to delete Content Control in Word document using OpenXML Format SDK? RRS feed

  • Question

  • Hi,

    How to delete Content Control in Word document using OpenXML Format SDK?


    Selvam S

    Thursday, October 8, 2015 11:40 AM

Answers

  • Hi Selvam,

    >> How to delete Content Control in Word document using OpenXML Format SDK?

    For deleting content control with OpenXML SDK, you could loop the SdtBlock element in Body, and remove them.
    Here is a simple code:

    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace OfficeAPI.Class.OpenXmlHelper.WordHelper
    {
        class DeleteContentControl
        {
            WordprocessingDocument WordDoc;
            public void RemoveContentControl(string filePath)
            {
                using (WordDoc = WordprocessingDocument.Open(filePath,true))
                {
                    MainDocumentPart main = WordDoc.MainDocumentPart;
                    SdtBlock[] sdtBlock = main.Document.Body.Descendants<SdtBlock>().ToArray();
                    foreach (SdtBlock sdt in sdtBlock)
                    {
                        sdt.Remove();
                    }
                }
            }
        }
    }
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, October 9, 2015 2:54 AM