After deleting WebExtensionPart from WordprocessingDocument document get corrupted

360 views Asked by At

I am new to open xml sdk and i haven't much idea on how relationship work for wordprocessing document. I want to remove webtask-pane from my existing document which contains webtask-pane and its added through programmatically.

After deleting WebExtensionPart from WordprocessingDocument using open office xml.

Result : document get corrupted. Reason : Relationship part is exists after deleting WebExtensionPart.

Code :

public static void RemoveTaskPaneExt(WordprocessingDocument package)
    {
        WebExTaskpanesPart webExTaskpanesPart1 = package.GetPartsOfType<WebExTaskpanesPart>().FirstOrDefault();

        if (webExTaskpanesPart1 != null)
        {
            WebExtensionPart aWebExtension =
                webExTaskpanesPart1.GetPartsOfType<WebExtensionPart>()
                    .Where(
                        x =>
                            x.WebExtension.WebExtensionStoreReference.Id ==
                            System.Configuration.ConfigurationManager.AppSettings["PaneID"])
                    .FirstOrDefault();
            if (aWebExtension != null)
            {
                bool result = package.WebExTaskpanesPart.DeletePart(aWebExtension);
            }                
        }
    }

Please help. Thanks in advance.

2

There are 2 answers

0
Taterhead On

The following will remove all WebExtensionTaskpanes and your Word file will be valid. Call this instead of your method above.

private static void RemoveWebExtensionPart(WordprocessingDocument package)
    {
        WebExTaskpanesPart webExTaskpanesPart1 = package.GetPartsOfType<WebExTaskpanesPart>().FirstOrDefault();

        if (webExTaskpanesPart1 != null)
        {
            bool result2 = package.DeletePart(webExTaskpanesPart1);
        }
    }
0
Hardik Shah On

I got the answer using removing all children of task pane.

public static void RemoveTaskPaneExt(WordprocessingDocument package)
    {
        WebExTaskpanesPart webExTaskpanesPart1 = package.GetPartsOfType<WebExTaskpanesPart>().FirstOrDefault();

        if (webExTaskpanesPart1 != null)
        {
            WebExtensionPart aWebExtension =
                webExTaskpanesPart1.GetPartsOfType<WebExtensionPart>()
                    .Where(
                        x =>
                            x.WebExtension.WebExtensionStoreReference.Id ==
                            System.Configuration.ConfigurationManager.AppSettings["PaneID"])
                    .FirstOrDefault();
            if (aWebExtension != null)
            {
webExTaskpanesPart1.Taskpanes.RemoveAllChildren();
                bool result = package.WebExTaskpanesPart.DeletePart(aWebExtension);
            }                
        }
}

Happy coding!