0
votes

I am using the following code to add a new page in existing document.It is working but the newly created page copying the properties of old page ( header and footer).I wanted to add/append a new page without header and footer to existing document. Please suggest what is wrong here.Is there any way to get the last page and remove the header and footer

Below code is giving this xml

        Paragraph paragraph = new Paragraph();
        ParagraphProperties paragraphProperties = new ParagraphProperties();

        SectionProperties sectionProperties1 = new SectionProperties();

        SectionType sectionType1 = new SectionType() { Val = SectionMarkValues.NextPage };
        //sectionType1.Descendants<FooterReference>();

        sectionProperties1.Append(sectionType1);
        paragraphProperties.Append(sectionProperties1);

        paragraph.Append(paragraphProperties);
        mainPart.Document.Body.InsertAfter(paragraph, mainPart.Document.Body.LastChild);
2

2 Answers

0
votes

Header and footer are connected with sections using relationships, like this:

<w:sectPr>
. . .
<w:footerReference r:id="rId10" w:type="default"/>
. . .
</w:sectPr>

So in order to remove the header/footer from section, one need to remove them from SectionProperties element. For all sections except the last section, the SectionProperties element is stored as a child element of the last paragraph in the section. For the last section, the SectionProperties is stored as a child element of the body element. So in order to add page without header and footer you need to:

  1. Create new paragraph
  2. Get last section properties from the Body and insert it's clone into new paragraph (by doing: secProp.Clone(true))
  3. Remove last section properties from the Body
  4. Add newly created paragraph to the Body
  5. Add new paragraph with page break to the body (new Paragraph(new Run(new Break(){ Type = BreakValues.Page })))
  6. Add new paragraph with new empty section properties as you already did in your code

For more info, see here: http://officeopenxml.com/WPsection.php

0
votes

I know this is a very old question but it's still unsolved and maybe someone else is searching for an answer. The cause of it is that Word automatically set the header and footer to "take from previous" if it's not the first section in the document. Even if no header and footer is set.

See documention of headerreference class (https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.headerreference):

If any type of header is omitted for a given section, then the following rules shall apply.

  • If no headerReference for the first page header is specified and the titlePg element is specified, then the first page header shall be inherited from the previous section or, if this is the first section in the document, a new blank header shall be created. If the titlePg element is not specified, then no first page header shall be shown, and the odd page header shall be used in its place.
  • If no headerReference for the even page header is specified and the evenAndOddHeaders element is specified, then the even page header shall be inherited from the previous section or, if this is the first section in the document, a new blank header shall be created. If the evenAndOddHeaders element is not specified, then no even page header shall be shown, and the odd page header shall be used in its place.
  • If no headerReference for the odd page header is specified then the even page header shall be inherited from the previous section or, if this is the first section in the document, a new blank header shall be created.

So this is the cause why header and footer are still showing on the last page.