Dodaj nagłówek i stopkę do istniejącego pustego dokumentu słownego za pomocą OpenXML SDK 2.0

Próbuję dodać nagłówek i stopkę do pustego dokumentu słowa.

Używam tego kodu, aby dodać część nagłówka w słowie / document.xml podczas zmiany docx na zip.

        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 the document
            // which is where header references are defined.
            foreach (SectionProperties sectProperties in
              mainDocPart.Document.Descendants<SectionProperties>())
            {
                //  Delete any existing references to headers.
                foreach (HeaderReference headerReference in
                  sectProperties.Descendants<HeaderReference>())
                    sectProperties.RemoveChild(headerReference);

                //  Create a new header reference that points to the new
                // header part and add it to the section properties.
                HeaderReference newHeaderReference =
                  new HeaderReference() { Id = rId, Type = HeaderFooterValues.First };
                sectProperties.Append(newHeaderReference);
            }

            //  Save the changes to the main document part.
            mainDocPart.Document.Save();
        }

    private static Header GeneratePageHeaderPart(string HeaderText)
    {
        var element =
          new Header(
            new Paragraph(
              new ParagraphProperties(
                new ParagraphStyleId() { Val = "Header1" }),
              new Run(
                new Text(HeaderText))
            )
          );

        return element;
    }

Ten kod działa, ale w słowie / _rels / document.xml.rels nie ma powiązania nagłówka.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
    <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
    <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
</Relationships>

I już nie nagłówek typu zawartości w [Content_Types] .xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
    <Default Extension="xml" ContentType="application/xml"/>
    <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
    <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
    <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
    <Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>
    <Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
    <Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>
    <Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/>
    <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
</Types>

Niemniej jednak header.xml został stworzony słowem /

<?xml version="1.0" encoding="utf-8"?><w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p><w:pPr><w:pStyle w:val="Header1" /></w:pPr><w:r><w:t>Test1</w:t></w:r></w:p></w:hdr>

I moje słowo / document.xml, aby pokazać, że mój nagłówek jest dodany.

<w:sectPr w:rsidR="003310CE" w:rsidSect="00D928B6">
<w:pgSz w:w="11906" w:h="16838" />
<w:pgMar w:top="1417" w:right="1417" w:bottom="1417" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />
<w:cols w:space="708" /><w:docGrid w:linePitch="360" />
<w:headerReference w:type="first" r:id="Recfa318e6a7c44ff" />
</w:sectPr>

Więc moje pytanie brzmi: jak poprawnie dodać nagłówek i stopkę?

Dzięki za pomoc.

questionAnswers(3)

yourAnswerToTheQuestion