Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i want to delete the header and footer in my document, but dont want to edit existing document only read from it in some memorystream and then remove footer and header of the the document?
C#
using (WordprocessingDocument doc = WordprocessingDocument.Open(file, true))


                                        {

                                            var docPart = doc.MainDocumentPart;

                                            // Count the header and footer parts and continue if there
                                            // are any.
                                            if (docPart.HeaderParts.Count() > 0 ||
                                                docPart.FooterParts.Count() > 0)
                                            {
                                                docPart.DeleteParts(docPart.HeaderParts);
                                                docPart.DeleteParts(docPart.FooterParts);
                                                Document document = docPart.Document;

                                                // Remove all references to the headers and footers.

                                                // First, create a list of all descendants of type
                                                // HeaderReference. Then, navigate the list and call
                                                // Remove on each item to delete the reference.
                                                var headers =
                                                  document.Descendants<HeaderReference>().ToList();
                                                foreach (var header in headers)
                                                {
                                                    header.Remove();
                                                }

                                                // First, create a list of all descendants of type
                                                // FooterReference. Then, navigate the list and call
                                                // Remove on each item to delete the reference.
                                                var footers =
                                                  document.Descendants<FooterReference>().ToList();
                                                foreach (var footer in footers)
                                                {
                                                    footer.Remove();
                                                }


                                                // Save the changes.
                                                document.Save();
                                            }

                                        }


Right now my code Removes header and footer of existing file.
Posted
Comments
Kornfeld Eliyahu Peter 10-Feb-15 5:47am    
So what the question is?

1 solution

Best option : make a replica of ur existing file and do the same code you have written above for that replica, so existing file will not get affected
 
Share this answer
 
Comments
Kurac1 10-Feb-15 6:27am    
Alright do u have some example?
Kurac1 10-Feb-15 6:31am    
i get that the file already exists there?
Nivedita_Parihar 11-Feb-15 4:44am    
I didn't get u.U manually have to make the copy of existing file that how you create a new one just alter that file with your above written code..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900