wordpress hit counter
Delete footnote/endnote from word document using System.IO.Packaging API - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Delete footnote/endnote from word document using System.IO.Packaging API

Delete footnote/endnote from word document using System.IO.Packaging API

  • Comments 11

Article by Mallika Biswas, Sonata Software Limited

 

This article explains how to delete footnotes and endnotes from an existing word document using System.IO.Packaging API in C# project.

 

Steps to be followed:

 

1. Create a new C# project. Go to references, add the WindowsBase.dll.

·         Go to Add reference dialog box and go to browse tab.

·         From C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 add WindowsBase.dll

 

2. Import the namespace:

  using System.IO.Packaging

 

3. Open the word document from where footnotes and endnotes to be removed.

          pack =Package.Open(filepath,FileMode.Open, FileAccess.ReadWrite);

 

4. Get the main document  part.

                  foreach (System.IO.Packaging.PackageRelationship relationship in                               pack.GetRelationshipsByType(documentRelationshipType))

            {

                documentUri = PackUriHelper.ResolvePartUri(new Uri("/",   UriKind.Relative), relationship.TargetUri);

 

                documentPart = pack.GetPart(documentUri);

                break;//only one part

 

            }

 

5. Load the document.xml file.

             

            xdoc.Load(documentPart.GetStream());

6. Search for the “w:footnotereference”  and “w:endnotereference”  element in document.xml.

            XmlNodeList footnodes = xdoc.SelectNodes("//" + footref, nsManager);

            if (footnodes.Count ==0 && endnotes.Count==0)

            {

                MessageBox.Show("no footnotes and endnotes to remove");

            }

 

 7. Remove  footnotereference  and endnotereference    element  from  document.xml:

                 foreach (XmlNode footnode in footnodes)

                {

                    XmlNode pnode = footnode.ParentNode;

                    pnode.RemoveChild(footnode);

                }

foreach (XmlNode endnote in endnotes)

                {

                    XmlNode enode = endnote.ParentNode;

                    enode.RemoveChild(endnote);

                }

 

8. Remove footnote part and relationship from  the document part.

foreach (System.IO.Packaging.PackageRelationship footrels in documentPart.GetRelationshipsByType(footnoterels))

                {

                    Uri footnoteuri = PackUriHelper.ResolvePartUri(documentUri, footrels.TargetUri);

                    footnoteids[no2] = footrels.Id;//here ids are taken for deleting relationship//

                                                                       

                    ++no2;       

                  pack.DeletePart(footnoteuri);

                    pack.Flush();

 

 

                }

             pack.Flush();

                for (int i = 0; i < no2; i++)

                {

                    documentPart.DeleteRelationship(footnoteids[i]);

                }

 

        

9. Remove endnote part and relationship from the document part.

foreach (System.IO.Packaging.PackageRelationship endrels in documentPart.GetRelationshipsByType(endnoterels))

                {

                    Uri endnoteuri = PackUriHelper.ResolvePartUri(documentUri, endrels.TargetUri);

                    endnoteids[no] = endrels.Id;

                    ++no;

                    pack.DeletePart(endnoteuri);

                    pack.Flush();

 

 

                }

  for (int i = 0; i < no; i++)

                {

                    documentPart.DeleteRelationship(endnoteids[i]);

                }

10. Save the document part.

xdoc.Save(documentPart.GetStream(FileMode.Create, FileAccess.Write));

       

11.Get the settings part from the document part.

foreach (System.IO.Packaging.PackageRelationship setrels in documentPart.GetRelationshipsByType(settingsrels))

                {

 

                    settingsuri = PackUriHelper.ResolvePartUri(documentUri, setrels.TargetUri);

 

                    settingspart = pack.GetPart(settingsuri);

                    break;

 

                }

 

12. Load the settings.xml

setdoc.Load(settingspart.GetStream());

 

13. Remove “w:footnotePr” and “w:endnotePr” from settings.xml part.

  foreach (System.Xml.XmlNode docfootnode in docfootnodes)

                {

                    XmlNode parentNode = docfootnode.ParentNode;

                    parentNode.RemoveChild(docfootnode);

 

                }

foreach (System.Xml.XmlNode endnode in endnodes)

                {

 

                    XmlNode parentNode2 = endnode.ParentNode;

                    parentNode2.RemoveChild(endnode);

 

                }

14. Save the part and close the package.

setdoc.Save(settingspart.GetStream());

pack.close();

 

This sample demo demonstates use of System.IO.Packaging API to delete footnotes and end notes using C# project. The example demo is attached with the article as a zip file.

 

 

Attachment: footnoteremoval.zip
Page 1 of 1 (11 items)