java - How to set plain header in docx file using apache poi? -


i create header docx document using apache poi have difficulties. have no working code show. ask piece of code starting point.

there's apache poi unit test covers case - you're looking testxwpfheader#testsetheader(). covers starting document no headers or footers set, adding them

your code like:

xwpfheaderfooterpolicy policy = sampledoc.getheaderfooterpolicy(); if (policy.getdefaultheader() == null && policy.getfirstpageheader() == null        && policy.getdefaultfooter() == null) {    // need create new headers    // easy way, gives single empty paragraph    xwpfheader headerd = policy.createheader(policy.default);    headerd.getparagraphs(0).createrun().settext("hello header world!");     // or full control way     ctp ctp1 = ctp.factory.newinstance();     ctr ctr1 = ctp1.addnewr();     cttext t = ctr1.addnewt();     t.setstringvalue("paragraph in header");      xwpfparagraph p1 = new xwpfparagraph(ctp1, sampledoc);     xwpfparagraph[] pars = new xwpfparagraph[1];     pars[0] = p1;      policy.createheader(policy.first, pars); } else {    // has header, change } 

see xwpfheaderfooterpolicy javadocs bit more on creating headers , footers.

it isn't nicest, ideally use kind soul submitting patch make nicer (hint hint...!), can work unit tests show


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -