]> source.dussan.org Git - poi.git/commitdiff
Added overloaded method to support multiple paragraphs in footer/header
authorMark Beardsley <msb@apache.org>
Mon, 24 May 2010 15:55:30 +0000 (15:55 +0000)
committerMark Beardsley <msb@apache.org>
Mon, 24 May 2010 15:55:30 +0000 (15:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@947686 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java

index 41f47836f375e4844bb3b4106464d5b8cece5cec..11c934f99e609c6986a02d637577340a3989b7da 100644 (file)
@@ -174,6 +174,8 @@ public class XWPFHeaderFooterPolicy {
                outputStream.close();
        return wrapper;
     }
+    
+    
 
     public XWPFFooter createFooter(Enum type) throws IOException {
        return createFooter(type, null);
@@ -214,14 +216,16 @@ public class XWPFHeaderFooterPolicy {
 
 
        private CTHdrFtr buildFtr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
-               CTHdrFtr ftr = buildHdrFtr(pStyle, pars);
+               //CTHdrFtr ftr = buildHdrFtr(pStyle, pars);                             // MB 24 May 2010
+               CTHdrFtr ftr = buildHdrFtr(pStyle, pars, wrapper);              // MB 24 May 2010
        setFooterReference(type, wrapper);
                return ftr;
        }
 
 
        private CTHdrFtr buildHdr(Enum type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) {
-               CTHdrFtr hdr = buildHdrFtr(pStyle, pars);
+               //CTHdrFtr hdr = buildHdrFtr(pStyle, pars);                             // MB 24 May 2010
+               CTHdrFtr hdr = buildHdrFtr(pStyle, pars, wrapper);              // MB 24 May 2010
        setHeaderReference(type, wrapper);
                return hdr;
        }
@@ -231,7 +235,40 @@ public class XWPFHeaderFooterPolicy {
                if (paragraphs != null) {
                        for (int i = 0 ; i < paragraphs.length ; i++) {
                                CTP p = ftr.addNewP();
-                               ftr.setPArray(0, paragraphs[i].getCTP());
+                               //ftr.setPArray(0, paragraphs[i].getCTP());             // MB 23 May 2010
+                               ftr.setPArray(i, paragraphs[i].getCTP());       // MB 23 May 2010
+                       }
+               }
+               else {
+                       CTP p = ftr.addNewP();
+                       byte[] rsidr = doc.getDocument().getBody().getPArray()[0].getRsidR();
+                       byte[] rsidrdefault = doc.getDocument().getBody().getPArray()[0].getRsidRDefault();
+                       p.setRsidP(rsidr);
+                       p.setRsidRDefault(rsidrdefault);
+                       CTPPr pPr = p.addNewPPr();
+                       pPr.addNewPStyle().setVal(pStyle);
+               }
+               return ftr;
+       }
+       
+       /**
+        * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
+        * that the XWPFFooter or XWPFHeader object returned by calls to the createHeader(int, XWPFParagraph[])
+        * and createFooter(int, XWPFParagraph[]) methods or the getXXXXXHeader/Footer methods where
+        * headers or footers had been added to a document since it had been created/opened, returned
+        * an object that contained no XWPFParagraph objects even if the header/footer itself did contain
+        * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.newInstance(); 
+        * created a brand new instance of the CTHDRFtr class which was then populated with data when
+        * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
+        * that had previoulsy been instantiated in the createHeader(int, XWPFParagraph[]) or 
+        * createFooter(int, XWPFParagraph[]) methods.
+        */
+       private CTHdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper) {
+               CTHdrFtr ftr = wrapper._getHdrFtr();
+               if (paragraphs != null) {
+                       for (int i = 0 ; i < paragraphs.length ; i++) {
+                               CTP p = ftr.addNewP();
+                               ftr.setPArray(i, paragraphs[i].getCTP());
                        }
                }
                else {