close
close
Open XML: Adding Shapes to Word Footers

Open XML: Adding Shapes to Word Footers

2 min read 09-11-2024
Open XML: Adding Shapes to Word Footers

Adding shapes to the footers of Word documents using Open XML can enhance the visual presentation of your documents. This guide will provide an overview of how to implement this using Open XML SDK.

Understanding Open XML

Open XML is a file format specification that allows for the manipulation of Microsoft Office documents. It is an XML-based standard that can be used to programmatically create and modify Word, Excel, and PowerPoint files.

Steps to Add Shapes to Word Footers

Step 1: Setting Up the Environment

Before you begin, ensure you have the following:

  • Open XML SDK installed.
  • A basic understanding of C# programming.

Step 2: Create a New Word Document

To add shapes to footers, you first need to create or open an existing Word document. Here is a simple snippet to create a new document:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

public void CreateWordDocument(string filepath)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
    {
        // Add a main document part.
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
        mainPart.Document = new Document();
        mainPart.Document.Append(new Body());
    }
}

Step 3: Add a Footer

To add a footer where shapes will be placed:

public void AddFooter(WordprocessingDocument wordDoc)
{
    FooterPart footerPart = wordDoc.MainDocumentPart.AddNewPart<FooterPart>();
    footerPart.Footer = new Footer(new Paragraph(new Run(new Text("Footer Text"))));
    footerPart.Footer.Save();

    var footerReference = new FooterReference() { Type = HeaderFooterValues.Default, Id = wordDoc.MainDocumentPart.GetIdOfPart(footerPart) };
    wordDoc.MainDocumentPart.Document.Body.Append(new FooterReference() { Id = footerPart.GetIdOfPart(footerPart) });
    wordDoc.MainDocumentPart.Document.Save();
}

Step 4: Adding Shapes to the Footer

Shapes can be added by utilizing the Drawing element within the footer. Here's a basic example of how to add a simple shape:

public void AddShapeToFooter(FooterPart footerPart)
{
    var drawing = new Drawing(
        new DW.Inline(
            new DW.Extent() { Cx = 1000000, Cy = 1000000 },
            new DW.EffectExtent() { LeftEdge = 0, RightEdge = 0, TopEdge = 0, BottomEdge = 0 },
            new DW.WrapNone(),
            new DW.DocProperties() { Id = (UInt32Value)1U, Name = "Shape 1" },
            new DW.NonVisualGraphicFrameDrawingProperties(
                new A.GraphicFrameLocks() { NoChangeAspect = true }),
            new A.Graphic(
                new A.GraphicData(
                    new A.Shape(
                        new A.NonVisualShapeProperties(
                            new A.NonVisualDrawingProperties() { Id = 2U, Name = "My Shape" },
                            new A.NonVisualShapeDrawingProperties()),
                        new A.ShapeProperties(),
                        new A.TextBody(
                            new A.BodyProperties(),
                            new A.ListStyle(),
                            new A.Paragraph(new A.Run(new A.Text("Hello, Shape!"))))
                    )
                ) { Tn = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
        )
    );

    footerPart.Footer.Append(drawing);
    footerPart.Footer.Save();
}

Step 5: Combine It All

Now that you have the individual components, you can combine them into a single method to create the Word document with the footer and the shape.

public void CreateDocumentWithFooterAndShape(string filepath)
{
    CreateWordDocument(filepath);
    
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filepath, true))
    {
        AddFooter(wordDoc);
        var footerPart = wordDoc.MainDocumentPart.FooterParts.First();
        AddShapeToFooter(footerPart);
    }
}

Conclusion

By following the above steps, you can successfully add shapes to the footers of Word documents using Open XML. This process can be customized further based on your specific design and content requirements. Remember to always refer to the Open XML documentation for more advanced shapes and styling options.

Popular Posts