Add Header and Footer to an existing empty word document with OpenXML SDK 2.0

Add Header and Footer to an existing empty word document with OpenXML SDK 2.0

To add a header and footer to an existing empty Word document using the OpenXML SDK 2.0 in C#, you can follow these steps:

  1. Create a new WordprocessingDocument object using the OpenXML SDK.
  2. Add a MainDocumentPart to the WordprocessingDocument.
  3. Add a Body element to the MainDocumentPart.
  4. Add a SectionProperties element to the Body element.
  5. Add a HeaderReference and FooterReference element to the SectionProperties element, with appropriate reference Ids.
  6. Create a new HeaderPart and FooterPart for the header and footer, respectively.
  7. Add the header and footer to the WordprocessingDocument.
  8. Save the WordprocessingDocument.

Here is some sample code that demonstrates this approach:

using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; public static void AddHeaderAndFooterToEmptyDocument(string filePath, string headerText, string footerText) { using (WordprocessingDocument document = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document)) { // Add a new main document part MainDocumentPart mainPart = document.AddMainDocumentPart(); mainPart.Document = new Document(new Body()); // Add a new section with header and footer references SectionProperties sectionProperties = new SectionProperties(new HeaderReference() { Type = HeaderFooterValues.Default, Id = "header" }, new FooterReference() { Type = HeaderFooterValues.Default, Id = "footer" }); mainPart.Document.Body.AppendChild(sectionProperties); // Create a new header and footer part HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>("header"); FooterPart footerPart = mainPart.AddNewPart<FooterPart>("footer"); // Add the header and footer markup to the header and footer parts GenerateHeaderPartContent(headerPart, headerText); GenerateFooterPartContent(footerPart, footerText); // Save changes to the document mainPart.Document.Save(); } } private static void GenerateHeaderPartContent(HeaderPart headerPart, string headerText) { Header header = new Header(); Paragraph paragraph = new Paragraph(new Run(new Text(headerText))); header.AppendChild(paragraph); headerPart.Header = header; } private static void GenerateFooterPartContent(FooterPart footerPart, string footerText) { Footer footer = new Footer(); Paragraph paragraph = new Paragraph(new Run(new Text(footerText))); footer.AppendChild(paragraph); footerPart.Footer = footer; } 

In this example, the AddHeaderAndFooterToEmptyDocument method creates a new WordprocessingDocument object and adds a MainDocumentPart to it. It then adds a Body element to the MainDocumentPart, along with a SectionProperties element that includes HeaderReference and FooterReference elements. The GenerateHeaderPartContent and GenerateFooterPartContent methods are used to create the header and footer markup and add them to the appropriate parts. Finally, the document is saved and closed.

You can call this method with the path to an existing empty Word document, along with the header and footer text that you want to add. Note that this code assumes that the document does not already have any content or markup, and that you have the necessary permissions to create and modify files in the specified directory.

Examples

  1. "OpenXML SDK 2.0 header and footer tutorial"

    • Code Implementation:
      using (WordprocessingDocument doc = WordprocessingDocument.Open("document.docx", true)) { // Add header var headerPart = doc.MainDocumentPart.AddNewPart<HeaderPart>(); var header = new Header(); // Add header content headerPart.Header = new Header(new Paragraph(new Run(new Text("Header Content")))); doc.MainDocumentPart.Document.Body.InsertBefore(new Paragraph(new Run(new Break())), doc.MainDocumentPart.Document.Body.FirstChild); // Add footer var footerPart = doc.MainDocumentPart.AddNewPart<FooterPart>(); var footer = new Footer(); // Add footer content footerPart.Footer = new Footer(new Paragraph(new Run(new Text("Footer Content")))); doc.MainDocumentPart.Document.Body.InsertBefore(new Paragraph(new Run(new Break())), doc.MainDocumentPart.Document.Body.FirstChild); } 
    • Description: This code opens an existing Word document and adds a header and footer with specified content.
  2. "OpenXML SDK 2.0 add header and footer to empty document"

    • Code Implementation:
      using (WordprocessingDocument doc = WordprocessingDocument.Create("document.docx", WordprocessingDocumentType.Document)) { // Add main document part var mainPart = doc.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); // Add header and footer parts var headerPart = mainPart.AddNewPart<HeaderPart>(); var footerPart = mainPart.AddNewPart<FooterPart>(); // Add header and footer to the document var header = new Header(new Paragraph(new Run(new Text("Header Content")))); var footer = new Footer(new Paragraph(new Run(new Text("Footer Content")))); headerPart.Header = header; footerPart.Footer = footer; } 
    • Description: This code creates a new empty Word document and adds a header and footer.
  3. "OpenXML SDK 2.0 C# code for setting header and footer styles"

    • Code Implementation:
      // Assuming headerPart and footerPart are already created var headerStyle = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "rId1" }; var footerStyle = new FooterReference() { Type = HeaderFooterValues.Default, Id = "rId2" }; var sectionProps = new SectionProperties(new HeaderReference() { Id = headerPart.RelationshipId, Type = HeaderFooterValues.Default }, new FooterReference() { Id = footerPart.RelationshipId, Type = HeaderFooterValues.Default }); 
    • Description: This code sets the header and footer styles for the document.
  4. "OpenXML SDK 2.0 paragraph formatting in header"

    • Code Implementation:
      // Assuming headerPart is already created var header = new Header(new Paragraph(new Run(new Text("Header Content")))); header.Descendants<Paragraph>().First().ParagraphProperties = new ParagraphProperties(new Justification() { Val = JustificationValues.Center }); headerPart.Header = header; 
    • Description: This code demonstrates how to format paragraphs within the header.
  5. "OpenXML SDK 2.0 remove header and footer from Word document"

    • Code Implementation:
      // Assuming headerPart and footerPart are already created headerPart.Header = new Header(); footerPart.Footer = new Footer(); 
    • Description: This code removes the header and footer from the Word document.
  6. "OpenXML SDK 2.0 add page number to footer"

    • Code Implementation:
      // Assuming footerPart is already created var footer = new Footer(new Paragraph(new Run(new FieldChar() { FieldCharType = FieldCharValues.Begin }), new Run(new RunProperties(new NoProof()), new FieldCode("PAGE"), new FieldChar() { FieldCharType = FieldCharValues.End }))); footerPart.Footer = footer; 
    • Description: This code adds a page number field to the footer.
  7. "OpenXML SDK 2.0 set different headers for odd and even pages"

    • Code Implementation:
      // Assuming headerPart is already created var oddHeader = new Header(new Paragraph(new Run(new Text("Odd Page Header")))); var evenHeader = new Header(new Paragraph(new Run(new Text("Even Page Header")))); headerPart.OddHeader = oddHeader; headerPart.EvenHeader = evenHeader; 
    • Description: This code sets different headers for odd and even pages.
  8. "OpenXML SDK 2.0 add image to header or footer"

    • Code Implementation:
      // Assuming headerPart or footerPart is already created var imagePart = headerPart.AddNewPart<ImagePart>("image/jpeg", "rId3"); using (var stream = new FileStream("image.jpg", FileMode.Open)) { imagePart.FeedData(stream); } var image = new Drawing(new Inline(new Extent() { Cx = 3000000, Cy = 3000000 }, new Graphic(new GraphicData(new Picture(new BlipFill(new DocumentFormat.OpenXml.Drawing.Blip() { Embed = "rId3" }), new Stretch(new FillRectangle())), new ShapeProperties(new Transform2D(new Offset() { X = 0, Y = 0 }, new Extents() { Cx = 3000000, Cy = 3000000 })))), new GraphicProperties(new NoFill())))); headerPart.Header = new Header(new Paragraph(new Run(image))); 
    • Description: This code adds an image to the header.
  9. "OpenXML SDK 2.0 add hyperlink to footer text"

    • Code Implementation:
      // Assuming footerPart is already created var hyperlink = new Hyperlink(new Run(new Text("Click me!"))) { Id = "rId4" }; var relationship = footerPart.AddHyperlinkRelationship(new Uri("https://example.com"), true, "rId4"); hyperlink.HyperlinkRelationship = relationship; var footer = new Footer(new Paragraph(hyperlink)); footerPart.Footer = footer; 
    • Description: This code adds a hyperlink to text in the footer.
  10. "OpenXML SDK 2.0 set different headers for first page"

    • Code Implementation:
      // Assuming headerPart is already created var firstPageHeader = new Header(new Paragraph(new Run(new Text("First Page Header")))); headerPart.FirstHeader = firstPageHeader; 
    • Description: This code sets a different header for the first page.

More Tags

board-games attributes bubble-chart hebrew dynamic-compilation clion tomcat8 c#-5.0 retrofit2 syntastic

More C# Questions

More Statistics Calculators

More Chemical reactions Calculators

More Stoichiometry Calculators

More Geometry Calculators