簡體   English   中英

僅針對使用 OpenXML 2 的現有 Word 文檔將頁眉添加/替換到首頁

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

我正在使用 OpenXML SDK 2.0 開發一個解決方案,以僅為文檔的第一頁添加或替換文檔標題部分。 我已將HeaderReference類型添加為HeaderFooterValues.First 但它沒有添加標題。 我沒有嘗試過,它正在將標題添加到每個頁面。 我使用的代碼在下面。

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 });
           }
        }
}

這有什么我做錯了嗎?

最后,我弄清楚了問題所在。

盡管我已將 HeaderReference 類型添加為 First,但除非您將 Title Page 屬性添加到 Section Properties 中,否則它不會被激活。 所以更新的代碼將是這樣的。

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 });
          }
       }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM