wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

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

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.

 

 

Published Sunday, February 24, 2008 11:39 PM by Sheela E N
Filed Under: , ,
Attachment(s): footnoteremoval.zip

Comments

No Comments
Anonymous comments are disabled