tibe
tibe

Reputation: 66

How to add a first page footer different from default one in NPOI - Docx?

I'm generating a docx file using NPOI 2.5.2 and I stuck with headers/footers for first page. I'd like to have a first page custom footer and start numbering pages from the second one.

Here is my code for the first page footer:

// First page
doc.Document.body.sectPr = new CT_SectPr();
var footer = new CT_Ftr();
var footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewT().Value = $"FIRST PAGE CUSTOM FOOTER";
var footerPar = new XWPFParagraph(footerParagraph, doc);
var parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
var headerFooterPolicy = doc.GetHeaderFooterPolicy();
if (headerFooterPolicy == null)
    headerFooterPolicy = doc.CreateHeaderFooterPolicy();
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.FIRST, parsFooter);

Here is my code for the default footer with page numbering:

// Other pages
footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
footerParagraph.AddNewR().AddNewInstrText().Value = " PAGE ";
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
footerPar = new XWPFParagraph(footerParagraph, doc);
parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

With the code above I could not see the first page custom footer but the page numbering in every page. What am I doing wrong?

I found this similar question but I could not find addNewTitlePg method in NPOI.

And is there any appropriate documentation with examples about NPOI?

Upvotes: 2

Views: 693

Answers (1)

lucas tadeu
lucas tadeu

Reputation: 11

Here is my code with the first footer working. (using NPOI 2.5.3)

var doc = new XWPFDocument();
using (var sw = File.Create("fileformat.docx"))
{
    
    XWPFParagraph p1 = doc.CreateParagraph();
    XWPFParagraph p2 = doc.CreateParagraph();
    XWPFRun r1 = p1.CreateRun();
    XWPFRun r2 = p2.CreateRun();


    r1.SetText("The quick brown fox");
    r1.AddBreak(BreakType.PAGE);
    r2.SetText("Next page: The quick brown fox");

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

    var policy = doc.CreateHeaderFooterPolicy();
    var ctSectPr = doc.Document.body.sectPr;
    if (ctSectPr.titlePg == null)
    {
        ctSectPr.titlePg = new CT_OnOff() { val = true };
    }
    var firstFooter = policy.CreateFooter(ST_HdrFtr.first);
    
    var paragraph = firstFooter.CreateParagraph();
    var run = paragraph.CreateRun();
    run.SetText("First page footer...");

    var defaultFooter = policy.CreateFooter(ST_HdrFtr.@default);

    paragraph = defaultFooter.CreateParagraph();
    run = paragraph.CreateRun();

    paragraph.Alignment = ParagraphAlignment.RIGHT;
    paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
    paragraph.GetCTP().AddNewR().AddNewInstrText().Value = " PAGE ";
    paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
    paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;

    run = paragraph.CreateRun();


    doc.Write(sw);
    
}

Upvotes: 1

Related Questions