相关疑难解决方法(0)

使用OpenXML SDK 2.0将页眉和页脚添加到现有的空单词文档中

我正在尝试在空文档中添加页眉和页脚.

当将docx更改为zip时,我使用此代码在word/document.xml中添加Header部分.

        ApplyHeader(doc);


      public static void ApplyHeader(WordprocessingDocument doc)
      {
            // Get the main document part.
            MainDocumentPart mainDocPart = doc.MainDocumentPart;

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

            // Create a new header part and get its relationship id.
            HeaderPart newHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
            string rId = mainDocPart.GetIdOfPart(newHeaderPart);

            // Call the GeneratePageHeaderPart helper method, passing in
            // the header text, to create the header markup and then save
            // that markup to the header part.
            GeneratePageHeaderPart("Test1").Save(newHeaderPart);

            // Loop through all section properties in …
Run Code Online (Sandbox Code Playgroud)

c# ms-word header footer openxml-sdk

12
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×1

footer ×1

header ×1

ms-word ×1

openxml-sdk ×1