To run the OOXML code you will need to reference quite a few extra archives I
am afraid, but before I get to that I had a thought in the middle of last
night. The developers are quite scrupulous in providing test code for
features as they are added to the API. Knowing that, I reasoned that there
must be some test code for the header footer policy and there is; I have
managed to locate it and include it below for I will be working away from
the office/workshop all day today and will not have time to test it out;

/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.xwpf.usermodel;

import java.io.IOException;

import junit.framework.TestCase;

import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

public final class TestXWPFHeader extends TestCase {

        public void testSimpleHeader() {
                XWPFDocument sampleDoc =
XWPFTestDataSamples.openSampleDocument("headerFooter.docx");

                XWPFHeaderFooterPolicy policy = 
sampleDoc.getHeaderFooterPolicy();


                XWPFHeader header = policy.getDefaultHeader();
                XWPFFooter footer = policy.getDefaultFooter();
                assertNotNull(header);
                assertNotNull(footer);

                // TODO verify if the following is correct
                assertNull(header.toString());

        }

        public void testSetHeader() throws IOException {
                XWPFDocument sampleDoc =
XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
                // no header is set (yet)
                XWPFHeaderFooterPolicy policy = 
sampleDoc.getHeaderFooterPolicy();
                assertNull(policy.getDefaultHeader());
                assertNull(policy.getFirstPageHeader());
                assertNull(policy.getDefaultFooter());

                CTP ctP1 = CTP.Factory.newInstance();
                CTR ctR1 = ctP1.addNewR();
                CTText t = ctR1.addNewT();
                t.setStringValue("Paragraph in header");

                CTP ctP2 = CTP.Factory.newInstance();
                CTR ctR2 = ctP2.addNewR();
                CTText t2 = ctR2.addNewT();
                t2.setStringValue("Second paragraph.. for footer");

                XWPFParagraph p1 = new XWPFParagraph(ctP1);
                XWPFParagraph[] pars = new XWPFParagraph[1];
                pars[0] = p1;

                XWPFParagraph p2 = new XWPFParagraph(ctP2);
                XWPFParagraph[] pars2 = new XWPFParagraph[1];
                pars2[0] = p2;

                // set a default header and test it is not null
                policy.createHeader(policy.DEFAULT, pars);
                policy.createHeader(policy.FIRST);
                policy.createFooter(policy.DEFAULT, pars2);

                assertNotNull(policy.getDefaultHeader());
                assertNotNull(policy.getFirstPageHeader());
                assertNotNull(policy.getDefaultFooter());
        }

        public void testSetWatermark() {
                XWPFDocument sampleDoc =
XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
                // no header is set (yet)
                XWPFHeaderFooterPolicy policy = 
sampleDoc.getHeaderFooterPolicy();
                assertNull(policy.getDefaultHeader());
                assertNull(policy.getFirstPageHeader());
                assertNull(policy.getDefaultFooter());

                policy.createWatermark("DRAFT");

                assertNotNull(policy.getDefaultHeader());
                assertNotNull(policy.getFirstPageHeader());
                assertNotNull(policy.getEvenPageHeader());
        }
}


There are still a few question to answer - I need to locate and look at the
test documents for example - but it should point a way forward for us.

Now, to get back to the archives, you will need to reference the following
on the classpath to compile and run the OOXML based code;

poi-3.6-beta1-20091007.jar
poi-scratchpad-3.6-beta1-20091007.jar
poi-ooxml-3.6-beta1-20091007.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
ooxml-schemas-1.0.jar


You will most likely not have archives with names that match exactly but do
not worry, the important part of the archives names is the poi-ooxml' part
not the date stamp, and of that list, I am not so sure you will need the
'scratchpad' jar, it is still hanging around from the HWPF test code.

If you cannot find these in the distribution that you downloaded, visit this
page;

http://encore.torchbox.com/poi-cvs-build/

where you will find a ong list that includes links to all of the archives
you need. If you have the flexibility, it may be better to download the very
latest version you can find such as - poi-3.7-SNAPSHOT-20100516.jar, etc.
>From experience, I know that even though this will contain pre-release code
and still be in active development. it is still stable enough to run safely
in all but the most demanding of production environments; though I would
still test everything throughly and very carefully of course and enforce a
split between devekopment and release in such a circumstance.

Hope this helps for now. I plan to have a play with some more code when I
get back home from work this evening. It had been my intention when we found
a solution to put together a small example showing how to insert/modify
headers and footers and then commit this to the POI website. If you make
significant progress before I, would you consider doing this also/instead?

Yours

Mark B



madhusudan.reddy wrote:
> 
> HI Mark,
> 
> Thanks for your help. I'm also trying to run your program but I'm unable
> to find the jars. I used POI-3.6 jar but I didn't find the required
> classes. Can you please provide the jar for below program? So that I can
> do some R & D on this issue.
> 
> Thanks & Regards,
> Madhusudan Reddy
> JAVA | HDC | Accenture
> M. No: 9966601155
> 
> -----Original Message-----
> From: MSB [mailto:[email protected]] 
> Sent: Sunday, May 16, 2010 8:54 PM
> To: [email protected]
> Subject: Re: Need help in updating footer of Word Document
> 
> 
> Only just begun to have a play with XWPF but it already seems much more
> 'solid' that HWPF as the latter is sorely in need of talented developers
> to
> help move it on (not too subtle hint there in case you missed it).
> 
> However, the news on footers - and on headers for that matter - is still
> not
> too good. The XWPFDocument class defines a method that allows you to
> recover
> the documents associated XWPFHeaderFooterPolicy object. That object is
> responsibile for managing the documents headers and footers and it
> contains
> method that allow you to create a new footer, recover a reference to a
> footer and to then modify that. The only problem I have run in to yet is
> that I cannot get at this object the XWPFHeaderFooterPolicy that is. So
> far,
> I have tried to call the gerHeaderFooterPolicy() on an XWPFDocument that I
> had previously created in the code and a null value was returned. Thinking
> about HWPF and the typical idion we use there when getting cells, I though
> that I should test the returned value and if it was null, then reate a new
> instance of the XWPFHeaderFooterPolicy class. This still did not work and
> the code still threw a NullPointerException. Finally, I though I would
> create a blank document using Word and insert a footer into that, planning
> to open it using POI and then get the header/footer policy and so on from
> there. However, now an exception was thrown when I tried to create an
> instance of the XWPFDocument class, this time the message concerned a null
> XWPFHeaderFooterPolicy object - surprise!
> 
> So, the next thing I am going to try is to download the very latest
> release
> of the software and try that. As always, I will keep you updated if I make
> any progress. If you want to follow in my footsteps so to speak, this is
> the
> scruffy test code I have been playing around with;
> 
> import org.apache.poi.xwpf.usermodel.*;
> import org.apache.poi.xwpf.model.*;
> 
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
> 
> /**
>  *
>  * @author win user
>  */
> public class Main {
> 
>     public void createDocument(String documentName, String documentFolder)
> {
>         File file = null;
>         FileInputStream fis = null;
>         FileOutputStream fos = null;
>         XWPFDocument document = null;
>         XWPFHeaderFooterPolicy hfPolicy = null;
>         XWPFFooter footer = null;
>         XWPFParagraph[] footerParas = null;
>         XWPFParagraph para = null;
>         XWPFRun run = null;
> 
>         try {
>             document = new XWPFDocument();
> 
>             para = document.createParagraph();
>             run = para.createRun();
>             run.setText("This should contain the text for the first
> XWPFRun
> " +
>                     "in the newly created Paragraph.");
>             run.setBold(true);
>             run = para.createRun();
>             run.setText("It is my hope that this XWPFRun will contain the
> "
> +
>                     "text for the second paragraph that I have just added
> -
> " +
>                     "with luck - to the newly created document.");
> 
>             hfPolicy = document.getHeaderFooterPolicy();
> 
>             if(hfPolicy == null) {
>                 hfPolicy = new XWPFHeaderFooterPolicy(document);
>             }
> 
>             hfPolicy.createFooter(
>                
> rg.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr.Enum.forString("Footer."));
> 
>             file = new File(documentFolder, documentName);
>             fos = new FileOutputStream(file);
>             document.write(fos);
>         }
>         catch(Exception ex) {
>             System.out.println("Caught an: " + ex.getClass().getName());
>             System.out.println("Message: " + ex.getMessage());
>             System.out.println("Stacktrace follows:.....");
>             ex.printStackTrace(System.out);
>         }
>         finally {
>             if(fos != null) {
>                 try {
>                     fos.close();
>                 }
>                 catch(IOException ioEx) {
>                     // I G N O R E
>                 }
>             }
>         }
> 
>     }
> 
>     /**
>      * @param args the command line arguments
>      */
>     public static void main(String[] args) {
>         new Main().createDocument("New XWPF Document.docx", "C:/temp");
>     }
> }
> 
> What it does show is how easy it is to use XWPF to create documents.
> 
> Yours
> 
> Mark B
> 
> 
> madhusudan.reddy wrote:
>> 
>> Hi,
>> 
>> We are using ApachePOI for creating word documents dynamically using java
>> in our application. We are wondering if we have a better way to handle
>> the
>> header and footers in the word document dynamically.
>> 
>> Need information or pointers to implement any of hte following things:
>> 1) Addition of new footer/header dynamically in the word document.
>> 2) Updating an existing footer/header dynamically in the word document.
>> 
>> Any pointers would be very helpful.
>> 
>> Thanks & Regards,
>> Madhusudan Reddy
>> JAVA | HDC | Accenture
>> M. No: 9966601155
>> 
>> 
>> This message is for the designated recipient only and may contain
>> privileged, proprietary, or otherwise private information.  If you have
>> received it in error, please notify the sender immediately and delete the
>> original.  Any other use of the email by you is prohibited.
>> 
>> 
> 
> -- 
> View this message in context:
> http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28575351.html
> Sent from the POI - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> 
> 
> 
> This message is for the designated recipient only and may contain
> privileged, proprietary, or otherwise private information.  If you have
> received it in error, please notify the sender immediately and delete the
> original.  Any other use of the email by you is prohibited.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28580187.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to