Search code examples
c#apache-poinpoi

NPOI Center-Align Footer in Microsoft Word Document


I have the following code, which inserts a footer into a Word document using the NPOI library for .Net Core. Currently, the footer inserts just fine, but I'm struggling to find any sort of examples on how to align the footer. Can anyone give an example or perhaps point me in the right direction?

XWPFDocument document = new XWPFDocument();

document.Document.body.sectPr = new CT_SectPr();

CT_Ftr footer = new CT_Ftr();
footer.AddNewP().AddNewR().AddNewT().Value = "Copyright © " + DateTime.Now.Year;
XWPFRelation footerRelation = XWPFRelation.FOOTER;
XWPFFooter documentFooter = (XWPFFooter)document.CreateRelationship(footerRelation, XWPFFactory.GetInstance(), document.FooterList.Count + 1);
documentFooter.SetHeaderFooter(footer);
CT_HdrFtrRef footerRef = document.Document.body.sectPr.AddNewFooterReference();
footerRef.type = ST_HdrFtr.@default;
footerRef.id = documentFooter.GetPackageRelationship().Id;

FileStream outStream = new FileStream("example.docx", FileMode.Create);
document.Write(outStream);

Solution

  • I figured it out! I'll leave my code here just in case anyone else has the same problem:

    CT_Ftr footer = new CT_Ftr();
    CT_P footerParagraph = footer.AddNewP();
    CT_PPr ppr = footerParagraph.AddNewPPr();
    CT_Jc align = ppr.AddNewJc();
    align.val = ST_Jc.center;
    

    I needed to add a ST_Jc to my paragraph. After adding a new run to footerParagraph, I was set!