Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Does anybody know how to add text to a footer using OpenXML? Unfortunately, knowing how to add text to the document part is not helping much since footer/headers are setup differently and don't seem to have corresponding parts.

What I have tried:

FooterPart footer = _doc.MainDocumentPart.FooterParts.ElementAtOrDefault(0);
Paragraph paragraph = footer.AddPart<paragraph>(paragraph);
Posted
Updated 13-Jan-23 10:02am

Good solution from Seergius96 on StackOverflow:

C#
doc = WordprocessingDocument.Open(_stream, true);

FooterPart footerPart = doc.MainDocumentPart.FooterParts.LastOrDefault();
footerPart.Footer.AppendChild(new Paragraph(new Run(new Text("CUI\n"))));
 
Share this answer
 
As shared here[^],
Quote:
The headers and footers aren't in the MainDocumentPart.Document, they are subparts in the HeaderParts and FooterParts[^] collections off the MainDocumentPart[^]. So you'll need to process each part separately. Fortunately, all of the parts share a common base class, OpenXmlPart[^], so you can put them together into a list and process them in a loop. Since you'll be dealing with the base class, you'll need to use RootElement[^] property instead of Document as a starting point to get the Descendants.

So the end result would look something like this:
C#
var partsToUpdate = new List<OpenXmlPart> { doc.MainDocumentPart }
    .Concat(doc.MainDocumentPart.HeaderParts)
    .Concat(doc.MainDocumentPart.FooterParts);

foreach (var part in partsToUpdate)
{
    var elements = part.RootElement
        .Descendants<SdtElement>()
        .Where(s => s.SdtProperties.ChildElements.Count > 0 &&
                    s.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag);

    foreach (var element in elements)
    {
        if (element == null)
        {
            continue;
        }

        var elementText = element.Descendants<Text>();

        if (elementText != null)
        {
            var elementTextValue = elementText.FirstOrDefault();

            if (elementTextValue != null)
            {
                elementTextValue.Text = text;
            }
            elementText.Skip(1).ToList().ForEach(t => t.Remove());
        }
    }
}
 
Share this answer
 
Comments
Darryl Bryk 10-Jan-23 22:03pm    
This solution looks like it will search all the doc. parts for a text and replace it with another text. What I want to do is append a text string to the current footer in a template document. This should look something like this code below which adds a child paragraph to the main document, however, I need to add a child paragraph to the footer part.

private static void AddText(WordprocessingDocument doc, string text) {
Paragraph paragraph = doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph());
Run run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(text));
run.PrependChild(new RunProperties());
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900