2012-07-26 7 views
9

Ich versuche, eine Kopf- und Fußzeile zu einem leeren Word-Dokument hinzuzufügen.Hinzufügen von Kopf- und Fußzeile zu einem vorhandenen leeren Word-Dokument mit OpenXML SDK 2.0

Ich benutze diesen Code, um Header-Teil in Word/document.xml hinzuzufügen, wenn Sie docx in zip ändern.

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

Dieser Code funktioniert, aber es gibt keine Kopf Beziehung in Wort erstellt/_rels/document.xml.rels.

<?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> 

Und nicht mehr Inhaltstyp-Header in [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> 

Dennoch header.xml in Wort/

<?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> 

Und mein Wort/document.xml erstellt wurde um zu zeigen, dass meine Kopfzeile hinzugefügt wurde.

<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> 

Also meine Frage ist, wie Header hinzufügen und richtig Fußzeile?

Danke für Ihre Hilfe.

Antwort

1

Ich finde die Lösung alleine.

In der Tat, ein Kopf- oder Fußzeile in einem docx mit OpenXml Zugabe beinhaltet, dass:

Dokument vorhanden sein muss.

Das Speichern und Schließen von Dokumenten vor dem Hinzufügen einer Kopf- oder Fußzeile funktioniert nicht.

WordprocessingDocument document = WordprocessingDocument.Open(path, true); 
<operations on docx> 
document.Save(); 
document.Close(); 
ChangeHeader(document); 

Sie müssen ein zweites Mal Ihr docx erneut öffnen, um eine neue Instanz des Dokuments zu erstellen.

wird Richtige Lösung (mit James Wood Lösung) sein:

WordprocessingDocument document = WordprocessingDocument.Open(path, true); 
<operations on docx> 
document.Save(); 
document.Close(); 
ChangeHeader(path); 

    public 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); 
      mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts); 

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

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

      GenerateHeaderPartContent(headerPart); 

      GenerateFooterPartContent(footerPart); 

      // 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>(); 
       section.RemoveAllChildren<FooterReference>(); 

       // Create the new header and footer reference node 
       section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId }); 
       section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId }); 
      } 
     } 
    } 


public static void GenerateHeaderPartContent(HeaderPart part) 
     { 
      Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; 
      header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); 
      header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); 
      header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); 
      header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); 
      header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); 
      header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); 
      header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); 
      header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); 
      header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); 
      header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); 
      header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); 
      header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); 
      header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); 
      header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); 
      header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); 

      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; 

      ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" }; 

      paragraphProperties1.Append(paragraphStyleId1); 

      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Header"; 

      run1.Append(text1); 

      paragraph1.Append(paragraphProperties1); 
      paragraph1.Append(run1); 

      header1.Append(paragraph1); 

      part.Header = header1; 
     } 

     public static void GenerateFooterPartContent(FooterPart part) 
     { 
      Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; 
      footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); 
      footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); 
      footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); 
      footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); 
      footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); 
      footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); 
      footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); 
      footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); 
      footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); 
      footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); 
      footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); 
      footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); 
      footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); 
      footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); 
      footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); 

      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; 

      ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" }; 

      paragraphProperties1.Append(paragraphStyleId1); 

      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Footer"; 

      run1.Append(text1); 

      paragraph1.Append(paragraphProperties1); 
      paragraph1.Append(run1); 

      footer1.Append(paragraph1); 

      part.Footer = footer1; 
     } 
22

Ich bin mir ziemlich sicher, was mit Ihrem Code falsch ist, ich vermute es ist die Art, wie Sie die Referenzen ändern.

In jedem Fall habe ich ein Arbeitsbeispiel, das Sie hoffentlich leiten sollten.

Ich habe anhand Mine aus den Beispielen hier: http://msdn.microsoft.com/en-us/library/office/cc546917.aspx

verwendete ich das Open XML SDK 2.0 Productivity Tool, um die Kopf- und Fußzeile Teile zu erzeugen. Ich erstelle zuerst ein Dokument mit meinem gewünschten Layout und öffne es dann mit dem Tool, es generiert den XML oder Code. Sie können es hier bekommen: http://www.microsoft.com/en-us/download/details.aspx?id=5124

Der einzige Vorbehalt zu diesem ist, dass es das Dokument bereits einige Inhalte im Körper annimmt, wird ein einzelner Brief tun. Ich bin mir nicht sicher, ob dies vermieden werden kann. Ich habe versucht, ein leeres Dokument im Productivity Tool zu öffnen, und es tritt derselbe Fehler auf - "Datei kann nicht geöffnet werden: Die Archivdatei kann nicht die Größe 0 haben". In meinem Beispiel wird es auf WordprocessingDocument.Open fehlschlagen.

Vielleicht müssen Sie im Falle eines leeren Dokuments zuerst einen Körper erstellen. In jedem Fall vermute ich, dass das Hauptziel dieser Frage darin besteht, eine Kopf- und Fußzeile hinzuzufügen, so dass ich denke, dass dies eine gültige Antwort ist.

Wenn Sie möchten, kann ich die tatsächlichen cs/Projektdateien bereitstellen.

Hoffe, das hilft.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Wordprocessing; 
using DocumentFormat.OpenXml; 

namespace HeaderFooterDocX 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ChangeHeader(@"C:\Users\James\Desktop\Document.docx"); 
     } 

     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); 
       mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts); 

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

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

       GenerateHeaderPartContent(headerPart); 

       GenerateFooterPartContent(footerPart); 

       // 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>(); 
        section.RemoveAllChildren<FooterReference>(); 

        // Create the new header and footer reference node 
        section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId }); 
        section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId }); 
       } 
      } 
     } 

     static void GenerateHeaderPartContent(HeaderPart part) 
     { 
      Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; 
      header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); 
      header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); 
      header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); 
      header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); 
      header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); 
      header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); 
      header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); 
      header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); 
      header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); 
      header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); 
      header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); 
      header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); 
      header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); 
      header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); 
      header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); 

      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; 

      ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" }; 

      paragraphProperties1.Append(paragraphStyleId1); 

      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Header"; 

      run1.Append(text1); 

      paragraph1.Append(paragraphProperties1); 
      paragraph1.Append(run1); 

      header1.Append(paragraph1); 

      part.Header = header1; 
     } 

     static void GenerateFooterPartContent(FooterPart part) 
     { 
      Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; 
      footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); 
      footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); 
      footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); 
      footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); 
      footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); 
      footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); 
      footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); 
      footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); 
      footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); 
      footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); 
      footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); 
      footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); 
      footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); 
      footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); 
      footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); 

      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; 

      ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" }; 

      paragraphProperties1.Append(paragraphStyleId1); 

      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Footer"; 

      run1.Append(text1); 

      paragraph1.Append(paragraphProperties1); 
      paragraph1.Append(run1); 

      footer1.Append(paragraph1); 

      part.Footer = footer1; 
     } 
    } 
} 
+0

dort Hallo! Haben Sie ähnlichen Code für Excel-Tabelle? Oder wenn Sie mir eine Richtung geben könnten, in der ich suchen kann? Kann wirklich nichts über das Ändern der Kopf-/Fußzeile durch OpenXML in C# finden. – epema

7

dieser Code funktionierte für mich. Berücksichtigen Sie:

  1. Header-Beziehung ID ist "R97" in meinem Beispiel. Vielleicht möchten Sie eine einzigartige RId auf eigene Faust entscheiden
  2. gleichen für Footer RelationshipId ("R98")
  3. Sowohl Header als auch Fußzeile haben einen hartcodierten Absatz mit etwas Text.
  4. Ich gehe davon aus einem Dokument existiert bereits

      using (WordprocessingDocument doc = WordprocessingDocument.Open(destination, true)) 
          { 
           var mainDocPart = doc.MainDocumentPart; 
           if (doc == null) { 
            mainDocPart = doc.AddMainDocumentPart(); 
           } 
    
           if (mainDocPart.Document == null) 
           { 
            mainDocPart.Document = new Document(); 
           } 
    
           ApplyHeader(doc); 
    
           ApplyFooter(doc); 
          } 
    

ApplyHeader Methode:

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

      HeaderPart headerPart1 = mainDocPart.AddNewPart<HeaderPart>("r97"); 



      Header header1 = new Header(); 

      Paragraph paragraph1 = new Paragraph(){ }; 



      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Header stuff"; 

      run1.Append(text1); 

      paragraph1.Append(run1); 


      header1.Append(paragraph1); 

      headerPart1.Header = header1; 



      SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault(); 
      if (sectionProperties1 == null) 
      { 
       sectionProperties1 = new SectionProperties() { }; 
       mainDocPart.Document.Body.Append(sectionProperties1); 
      } 
      HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" }; 


      sectionProperties1.InsertAt(headerReference1,0); 

     } 

ApplyFooter Methode:

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

      FooterPart footerPart1 = mainDocPart.AddNewPart<FooterPart>("r98"); 



      Footer footer1 = new Footer(); 

      Paragraph paragraph1 = new Paragraph() { }; 



      Run run1 = new Run(); 
      Text text1 = new Text(); 
      text1.Text = "Footer stuff"; 

      run1.Append(text1); 

      paragraph1.Append(run1); 


      footer1.Append(paragraph1); 

      footerPart1.Footer = footer1; 



      SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault(); 
      if (sectionProperties1 == null) 
      { 
       sectionProperties1 = new SectionProperties() { }; 
       mainDocPart.Document.Body.Append(sectionProperties1); 
      } 
      FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" }; 


      sectionProperties1.InsertAt(footerReference1, 0); 

     } 

Genießen,

Verwandte Themen