Sandun Isuru Niraj
Sandun Isuru Niraj

Reputation: 461

Adding/Replacing Header to First Page only for existing Word Document with OpenXML 2

I am working on a solution with OpenXML SDK 2.0 to add or replace the Document Header part only for the First Page of the Document. I have added HeaderReference type as HeaderFooterValues.First. But it is not adding the Header. I have tried without that and it is adding the Header to every page. The code I used is below here.

static void ChangeHeader(String documentPath)
{
      // Replace header in target document with header of source document.
      using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
      {
           // Get the main document part
           MainDocumentPart mainDocumentPart = document.MainDocumentPart;

           // Delete the existing header and footer parts
           mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);

          // Create a new header and footer part
          HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();

          // Get Id of the headerPart and footer parts
          string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);

          GenerateHeaderPartContent(headerPart);

          // Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
          IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();

          foreach (var section in sections)
          {
            // Delete existing references to headers and footers
            section.RemoveAllChildren<HeaderReference>();

            // Create the new header and footer reference node
            section.AppendChild<HeaderReference>(new HeaderReference() { Id = headerPartId, Type = HeaderFooterValues.First });
           }
        }
}

Is there anything I'm doing wrong in this?

Upvotes: 1

Views: 1083

Answers (1)

Sandun Isuru Niraj
Sandun Isuru Niraj

Reputation: 461

Finally, I have figured out what is the issue.

Even though I have added the HeaderReference Type as First, it is not activated unless you add the Title Page property to Section Properties. So Updated code will be like this.

static void ChangeHeader(String documentPath)
{
      // Replace header in target document with header of source document.
      using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
      {
           // Get the main document part
           MainDocumentPart mainDocumentPart = document.MainDocumentPart;

           // Delete the existing header parts
           mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);

          // Create a new header part
          HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();

          // Get Id of the headerPart
          string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);

          GenerateHeaderPartContent(headerPart);

          // Get SectionProperties and Replace HeaderReference with new Id
          IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();

          foreach (var section in sections)
          {
            // Delete existing references to headers
            section.RemoveAllChildren<HeaderReference>();

            //Adding Title Page property
            section.PrependChild<TitlePage>(new TitlePage());

            // Create the new header reference node
            section.AppendChild<HeaderReference>(new HeaderReference() { Id = headerPartId, Type = HeaderFooterValues.First });
          }
       }
}

Upvotes: 0

Related Questions