how i can identify the header footer type of a word document for each section/Page using openxml

60 views Asked by At

i have a document with header footer

  1. first page with different first page header
  2. second page with odd even page header
  3. third page with header footer

how i can identify that my first page have a different first page header or same for second and third page/Section in openxml.

enter image description here

1

There are 1 answers

0
ADITYA On

You can use the OpenXML SDK, Open your Word document using the WordprocessingDocument class from the OpenXML SDK.

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open("YourDocument.docx", false))
{
    // Your code
}

Iterate through the sections to access headers and footers.

foreach (SectionProperties sectionProperties in wordDoc.MainDocumentPart.Document.Body.Elements<SectionProperties>())
{
    // Your code
}

To check if a section has a different first page header,

var headerReference = sectionProperties.Elements<HeaderReference>().FirstOrDefault();
if (headerReference != null && headerReference.TitlePage != null)
{
    // Different first page header exists
}

To check for odd/even page headers,

var headerReference = sectionProperties.Elements<HeaderReference>().FirstOrDefault();
if (headerReference != null && headerReference.OddPage != null)
{
    // Odd page header exists
}

if (headerReference != null && headerReference.EvenPage != null)
{
    // Even page header exists
}