1

I have followed a few examples, but I simply can not get a footer to print to a word processing document using openxml. I am betting I am missing something trivial here, but I just can't see/find it.

            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body docBody = mainPart.Document.AppendChild(new Body());
            FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
            string footerId = mainPart.GetIdOfPart(footerPart);

            SectionProperties sectionProperties = new SectionProperties();
            FooterReference footerReference = new FooterReference() { Id = footerId, Type = HeaderFooterValues.Default };sectionProperties.Append(footerReference);
            PageMargin pageMargin = new PageMargin() { Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)1080U, Gutter = (UInt32Value)0U };
            sectionProperties.Append(pageMargin);

            Footer footer = new Footer();
            Paragraph paragraph50 = new Paragraph();
            ParagraphProperties footerProperties = new ParagraphProperties();

            footerProperties.Append(new ParagraphStyleId() { Val = "Footer" });
            footerProperties.Append(new Justification() { Val = JustificationValues.Both });
            footerProperties.Append(sectionProperties);
            paragraph50.Append(footerProperties);
            footer.Append(paragraph50);
            footerPart.Footer = footer;

            RunProperties frp = new RunProperties();
            frp.Color = new Color() { Val = "C0C0C0" };
            frp.FontSize = new FontSize() { Val = "15" };

            Run frun1 = new Run();
            frun1.Append(frp);
            frun1.Append(new Text("UNIFORM INSTRUMENT"));

            paragraph50.Append(frun1);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Jim
  • 13
  • 3

1 Answers1

0

The immediate problem is that you are appending the Section Properties to the footerPart when they should be appended to the docBody

i.e. if you change

        footerProperties.Append(sectionProperties);

to

        docBody.Append(sectionProperties);

you should then be able to open the resulting .docx and see the footer.

Personally, I find the Office Open SDK Productivity Tool invaluable for understanding how to build a Word document - WIndows only I think, but it is one of the downloads available here

  • Thank you for the help, it was not my only mistake, but ... The core error was the failure to include required namespace declarations – Jim Jun 29 '20 at 19:26
  • @Jim, I wondered about that, but when I ran your code, the lack of namespace definitions didn't seem to be a problem and the necessary definition ("w:", to keep it short) was in the output anyway. But there we are! –  Jun 29 '20 at 20:38