Adding footer to word ms using POI api

I've searched a lot and got some results that have some sample code but none seem to work. Everyone either gets a null pointer exception, or if the document is generated, then during the opening of the file (.docx) giving an error and displaying the message The text / xml declaration can occur only at the very beginning of work.

I thought I was adding some content and then adding a footer giving some problem, so I was pasting my footer code from the beginning, now this time I get

index from linked exception

Here is my complete code

String fileName ="Book.docx";
String   folderPath=SystemProperties.get(SystemProperties.TMP_DIR)+File.separator+"liferay" + File.separator    + "document_preview";
String filePath=folderPath+File.separator+fileName;
File file=new File(filePath);
XWPFDocument document = new XWPFDocument();  
XWPFParagraph paragraphOne = document.createParagraph();
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setText("Training Report");
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
            if (headerFooterPolicy == null) {
                CTBody body = document.getDocument().getBody();
                CTSectPr sectPr = body.getSectPr();
                if (sectPr == null) {
                sectPr = body.addNewSectPr();
                headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
            CTP ctP1 = CTP.Factory.newInstance();
            CTR ctR1 = ctP1.addNewR();
            CTText t = ctR1.addNewT();
            t.setStringValue("first footer");
            XWPFParagraph codePara = new XWPFParagraph(ctP1);
            XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
            newparagraphs[0] = codePara;
             XWPFFooter xwpfFooter = null;
    xwpfFooter =  headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
 FileOutputStream fileoutOfTraining = new FileOutputStream(file);
   downloadOperation(file, fileName, resourceResponse);


code in downloadOperation

HttpServletResponse httpServletResponse =PortalUtil.getHttpServletResponse(resourceResponse);
BufferedInputStream input = null;
        BufferedOutputStream output = null;
 httpServletResponse.setHeader("Content-Disposition", "attachment;    filename=\""+fileName+"\"; filename*=UTF-8''"+fileName);
        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
        } catch (FileNotFoundException e) {
        try {
            output = new BufferedOutputStream(httpServletResponse.getOutputStream(), DEFAULT_BUFFER_SIZE);
        } catch (IOException e) {
byte[] buffer = new byte[2*DEFAULT_BUFFER_SIZE];
        int length;
        try {
            while ((length = > 0) {
                output.write(buffer, 0, length);
        } catch (IOException e) {
        } finally {
            try {
            } catch (IOException e) {


Please help me to generate the footer, this is my code, if I add the footer code after my paragraph and table, then not a runtime error, but an error when opening the generated file if I put the footer before the content I want to add to in the docs I am getting the "index from linked exception" error. Please help me if anyone has a code snippet or at least some pointers to a solution. Thanks to


source to share

2 answers

I faced this problem and the solution We have to use the 3.10

final poi jar. 3.9

with this problem.

Delete jars of the previous version and add jars of the final version 3.10

, which fixed this error.

Banks require:

  1. poi-3,10-FINAL.jar

  2. poi-ooxml-3,10-final.jar

  3. poi-ooxml-schema-3,10-final.jar

Easily accessible online:

XWPFDocument document = new XWPFDocument();
CTP ctp = CTP.Factory.newInstance();
CTR ctr = ctp.addNewR();
CTRPr rpr = ctr.addNewRPr();
CTText textt = ctr.addNewT();
textt.setStringValue( " Page 1" );
XWPFParagraph codePara = new XWPFParagraph( ctp, document );
XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
newparagraphs[0] = codePara;
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new  XWPFHeaderFooterPolicy( document, sectPr );
headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );


The above code works fine, use the 3.10 wars I mentioned above.



I don't know if adding an image to the header from scratch in the new version, but I know that "templating" works just fine. I created a master document in word, adapted the heading the way I needed it (in my case, the logo image was on the right, a paragraph with dummy text on the left, and another image that separates the top two objects from the content on the bottom of the heading, and that's it repeated on all pages) and left the rest of the document blank. From the very beginning, I didn't create the XWPFDocument, calling XWPFDocument()

more, I created it like this:

XWPFDocument doc = new XWPFDocument(new FileInputStream("pathTo/template.docx"));


Instead of just populating your document with your content, and if you need to update the header text for another export like me, call the update header function, which in my case looks something like this:

public void updateHeader() throws InvalidFormatException, IOException {
        // load the header policy from template and update the paragraph text
        XWPFHeaderFooterPolicy headerFooterPolicy = document
        XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader();
                .setText(profileName, 0);
        // this is only to put some space between the content in the header and the real content
                .get(defaultHeader.getParagraphs().size() - 1)


As far as I know, this works since 3.10, and if you stumble upon some Java security issues, try the current nightly version where many of these security issues have been fixed. It even worked for me on Google App Engine.



All Articles