poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike McCullough <mike.mccullo...@gmail.com>
Subject XWPFFooter / XWPFHeader creation
Date Fri, 11 Jan 2013 16:28:17 GMT

I've been encountering problems trying to create a footer or header for the
DOCX format. Most of the examples that I have seen (including the example
ones from Apache POI) use the idea of a template file--however, either
there's not a way to copy only the XWPFHeaderFooterPolicy or I would have
to remove what's existing in the document template so that I'm able to
utilize headers this way. I will say that this method works, but is not, at
least in my opinion, the optimal way to create a header or footer.

So for the method for creating the HeaderFooterPolicy for the non-template


XWPFHeaderFooterPolicy headerFooterPolicy =
if (headerFooterPolicy == null) {

CTBody body = document.getDocument().getBody();

CTSectPr sectPr = body.getSectPr();

if (sectPr == null) {

sectPr = body.addNewSectPr();


headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);



I noticed that unless I did the above to create a SectPr, using the
XWPFHeaderFooterPolicy(XWPFDocument document) constructor would throw a
NullPointerException since the constructor just always assumes there's a
SectPr. This could provide a bug fix to other folks who may have
encountered this problem.

Now that I have a working XWPFHeaderFooterPolicy, theoretically speaking I
should be able to just call
headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, new
XWPFParagraph[]{...}) function to create a default footer (likewise for a
default header). However, when I take this approach, I'm encountering the
following problem when opening the docx file with Microsoft Word:

"A text/xml declaration may occur only at the very beginning of input.

Location: Part: /word/footer1.xml, Line: 2, Column: 192"

Upon inspection of this XML file, I see that the following problem:


<?xml version="1.0" encoding="UTF-8"?>
<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p><w:r><w:t>Test
Footer</w:t></w:r></w:p></w:ftr><?xml version="1.0" encoding="UTF-8"?>
<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p><w:r><w:t>Test


Which shows that POI is producing malformed XML (two lines of the same
thing), but I have not encountered why this is the case.

My code that produces the XWPFFooter is shown below:


CTP ctP1 = CTP.Factory.newInstance();
CTR ctR1 = ctP1.addNewR();
CTText text = ctR1.addNewT();
text.setStringValue("Test Footer);
XWPFParagraph paragraph= new XWPFParagraph(ctP1, document);

XWPFFooter footer =
headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, new


If there's anyone who's able to help with this particular problem, please
let me know. I am not necessarily interested in using the template approach
because I really believe that it's a bad solution, and would like to
understand either what I'm missing or shortcomings of the library which
will resort to me using the template approach.


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message