You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by vincent thibaut <th...@magillem.com> on 2010/07/20 18:32:42 UTC

Working with footer in XWPF

Hi all, 

 I am a new comer to both the OOXML and the POI implementation around it, so
questions may come from a bad understanding of both mechanism. I apologize
for that.

 

I am using poi-bin-3.6-20091214.

 

I am currently building a first example which consist of inserting a new
text line in an existing default footer. As I was not able to found how to
insert it my last attempt is to collect the existing paragraph then
rebuilding the footer from scratch.

I am however blocked by 2 points:

 

1-      How to remove the original default footer?

2-      The new footer is added but only contains the last line followed by
2 empty line above the original one, and I have no clue why. I checked my
newparagraphs variable which is properly initialized.

 

Following is my example code.

 

 

public static void main(String args[]) throws IOException {

            

            String text = "Test";

            File docxFile = new File("c:\\tmp\\wordupdater\\tmp.docx");

            FileInputStream finStream = new
FileInputStream(docxFile.getAbsolutePath());

            XWPFDocument doc = new XWPFDocument(finStream);

            XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();

 

            CTP ctP1 = CTP.Factory.newInstance();

            CTR ctR1 = ctP1.addNewR();

            CTText t = ctR1.addNewT();

            t.setStringValue(text);

            XWPFParagraph codePara = new XWPFParagraph(ctP1);

            

      

            XWPFParagraph[] existingPara =
doc.getHeaderFooterPolicy().getDefaultFooter().getParagraphs();

            XWPFParagraph[] newparagraphs = new
XWPFParagraph[existingPara.length +1 ];

            newparagraphs[0] = existingPara[0];

            newparagraphs[1] = codePara;

            newparagraphs[2] = existingPara[1];

            policy.createFooter(policy.DEFAULT, newparagraphs);

            FileOutputStream fileOut = new FileOutputStream(docxFile);

            doc.write(fileOut);

          fileOut.close();

            }

 

One additional question which cross my mind, as I could start from an empty
footer document and completely create it, is how to insert dynamic types
like pageNumber into a paragraph.

 

Thanks in advance,

Vincent