Quantcast

Need help in updating footer of Word Document

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Need help in updating footer of Word Document

madhusudan.reddy
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Need help in updating footer of Word Document

madhusudan.reddy
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
Can you tell us please which version of the file format you are targetting, the legacy binary one with HWPF or the newer OOXML one with XWPF?

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Need help in updating footer of Word Document

madhusudan.reddy
Hi,

The word document that we are targetting is 2003 office version and the POI version wise we are using legacy one with HWPF.

We can use XWPF also, it would he helpful even if you have any pointers around that.

Thanks
Madhu
________________________________________
From: MSB [[hidden email]]
Sent: Saturday, May 15, 2010 11:56 AM
To: [hidden email]
Subject: Re: Need help in updating footer of Word Document

Can you tell us please which version of the file format you are targetting,
the legacy binary one with HWPF or the newer OOXML one with XWPF?

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-tp28566396p28566525.html
Sent from the POI - User mailing list archive at Nabble.com.


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

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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Need help in updating footer of Word Document

Mark Beardsley
Personally, I do not have an idea of how to proceed with that but I will have the time to look over the weekend - Monaco Grand Prix to look forward to and I can never just watch....

You do need to be aware that HWPF is still an immature api and it could well not be possible to achieve what you require using it; but as I have just said, I do not know this to be the case just yet. Owing to the simpler, more transparent, file format it may well be the case that XWPF is the better option for you and I would certainly suggest looking into it for the future. As the .docx files are basically zipped xml, it is far easier too see how and where to path the api to add functionality.

Will post again if I manage to find anything out. It will not be quick however as I have another user to help and they are putting the finishing touches to an application currently but I should get back to you before the end of this weekend.

Yours

Mark B

madhusudan.reddy wrote
Hi,

The word document that we are targetting is 2003 office version and the POI version wise we are using legacy one with HWPF.

We can use XWPF also, it would he helpful even if you have any pointers around that.

Thanks
Madhu
________________________________________
From: MSB [markbrdsly@tiscali.co.uk]
Sent: Saturday, May 15, 2010 11:56 AM
To: user@poi.apache.org
Subject: Re: Need help in updating footer of Word Document

Can you tell us please which version of the file format you are targetting,
the legacy binary one with HWPF or the newer OOXML one with XWPF?

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-tp28566396p28566525.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org

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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
Quick update.

As it stands, HWPF will not allow you to add a new footer or header into a document nor modify the existing one(s). The HearderStories class is the one you should use to gain access to a documents headers and footers and it does include support for recovering the contents of the same. The javadoc for the class contains this sentence though;

"WARNING - you shouldn't change the headers or footers, as offsets are not yet updated!"

If you absolutely have to modify the documents footers and you must process the older binary file format then you do have options. The most obvious would be to use OLE to control an instance of Word itself but this technique will only work if you are running on Windows and it is quite slow. The other option would be to use OpenOffice and control an instance of that application via it's UNO interface. Whilst this option would not limit the platform you could run the code on, it is still quite slow in execution and there is a fairly steep learning curve to traverse if you want to use UNO. That interface was designed to support many different programming languages and so it is not quite as simple as creating an object and calling it's methods sadly.

Will press on now to look at XWPF.

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Need help in updating footer of Word Document

madhusudan.reddy
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:[hidden email]]
Sent: Sunday, May 16, 2010 8:54 PM
To: [hidden email]
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: [hidden email]
For additional commands, e-mail: [hidden email]




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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Shamal Karunarathne
There's HeaderStories in HWPF. You can extract headers and footers using
that. It's clean and clear. Tell me if you can't retrieve.

On Mon, May 17, 2010 at 3:06 PM, <[hidden email]> 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:[hidden email]]
> Sent: Sunday, May 16, 2010 8:54 PM
> To: [hidden email]
> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
>
> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
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:markbrdsly@tiscali.co.uk]
Sent: Sunday, May 16, 2010 8:54 PM
To: user@poi.apache.org
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org




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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by Shamal Karunarathne
Thanks, yes, we now that you can do that but the quesion was 'how can I modify existing footers and insert new footers into a document' not how can I read the contents of same.

Yours

Mark B

Shamal Karunarathne wrote
There's HeaderStories in HWPF. You can extract headers and footers using
that. It's clean and clear. Tell me if you can't retrieve.

On Mon, May 17, 2010 at 3:06 PM, <madhusudan.reddy@accenture.com> 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:markbrdsly@tiscali.co.uk]
> Sent: Sunday, May 16, 2010 8:54 PM
> To: user@poi.apache.org
> 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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>
>
>
> 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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Shamal Karunarathne
Oh, Sorry for misinterpretation. I'm still trying to handle reading a Word
Document fully. After that only I'll focus on involved in writing back. May
be your case will help me by then.

Yours,
Shamal

On Mon, May 17, 2010 at 3:27 PM, MSB <[hidden email]> wrote:

>
> Thanks, yes, we now that you can do that but the quesion was 'how can I
> modify existing footers and insert new footers into a document' not how can
> I read the contents of same.
>
> Yours
>
> Mark B
>
>
> Shamal Karunarathne wrote:
> >
> > There's HeaderStories in HWPF. You can extract headers and footers using
> > that. It's clean and clear. Tell me if you can't retrieve.
> >
> > On Mon, May 17, 2010 at 3:06 PM, <[hidden email]> 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:[hidden email]]
> >> Sent: Sunday, May 16, 2010 8:54 PM
> >> To: [hidden email]
> >> 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: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
> >>
> >>
> >> 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: [hidden email]
> >> For additional commands, e-mail: [hidden email]
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28580194.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
No apology is necessary but I do need to warn you that HWPF is still an immature API that is very much in need of support from developers - that is a less than subtle hint by the way.

The problem is that pretty soon after completing the first iteration of the API, the lead developer accepted a new job that involved him signing a non-disclosure agreement with Microsoft. As a result, he is prevented from even discussing the work he completed on the API and it has unfortunately languished somewhat. If you do want to get involved in developing the API futher, take a look at this page - http://poi.apache.org/hwpf/index.html where you can find a link to the document Microsoft published detailing the binary file format which would be a very good place to start.

Yours

Mark B

Shamal Karunarathne wrote
Oh, Sorry for misinterpretation. I'm still trying to handle reading a Word
Document fully. After that only I'll focus on involved in writing back. May
be your case will help me by then.

Yours,
Shamal

On Mon, May 17, 2010 at 3:27 PM, MSB <markbrdsly@tiscali.co.uk> wrote:

>
> Thanks, yes, we now that you can do that but the quesion was 'how can I
> modify existing footers and insert new footers into a document' not how can
> I read the contents of same.
>
> Yours
>
> Mark B
>
>
> Shamal Karunarathne wrote:
> >
> > There's HeaderStories in HWPF. You can extract headers and footers using
> > that. It's clean and clear. Tell me if you can't retrieve.
> >
> > On Mon, May 17, 2010 at 3:06 PM, <madhusudan.reddy@accenture.com> 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:markbrdsly@tiscali.co.uk]
> >> Sent: Sunday, May 16, 2010 8:54 PM
> >> To: user@poi.apache.org
> >> 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: user-unsubscribe@poi.apache.org
> >> For additional commands, e-mail: user-help@poi.apache.org
> >>
> >>
> >>
> >>
> >> 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: user-unsubscribe@poi.apache.org
> >> For additional commands, e-mail: user-help@poi.apache.org
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28580194.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

David Fisher
I'm joining Mark in his comments.

> No apology is necessary but I do need to warn you that HWPF is still an
> immature API that is very much in need of support from developers - that is
> a less than subtle hint by the way.

Anyone with the HWPF "itch", please, please, please step up and show us your "stuff".

If your bosses really want full HWPF then tell them about the benefits of joining an open source project - the opportunity to influence development of a well known and widely used library making it even better.

> The problem is that pretty soon after completing the first iteration of the
> API, the lead developer accepted a new job that involved him signing a
> non-disclosure agreement with Microsoft. As a result, he is prevented from
> even discussing the work he completed on the API and it has unfortunately
> languished somewhat. If you do want to get involved in developing the API
> futher, take a look at this page - http://poi.apache.org/hwpf/index.html
> where you can find a link to the document Microsoft published detailing the
> binary file format which would be a very good place to start.

On the page Mark refers to it talks about nagging a particular person. Instead an interested developer who has created patches for HWPF will need to get on the POI Dev list and start telling us what they have done. Repeatedly.

Regards,
Dave

>
> Yours
>
> Mark B
>
>
> Shamal Karunarathne wrote:
>>
>> Oh, Sorry for misinterpretation. I'm still trying to handle reading a Word
>> Document fully. After that only I'll focus on involved in writing back.
>> May
>> be your case will help me by then.
>>
>> Yours,
>> Shamal
>>
>> On Mon, May 17, 2010 at 3:27 PM, MSB <[hidden email]> wrote:
>>
>>>
>>> Thanks, yes, we now that you can do that but the quesion was 'how can I
>>> modify existing footers and insert new footers into a document' not how
>>> can
>>> I read the contents of same.
>>>
>>> Yours
>>>
>>> Mark B
>>>
>>>
>>> Shamal Karunarathne wrote:
>>>>
>>>> There's HeaderStories in HWPF. You can extract headers and footers
>>> using
>>>> that. It's clean and clear. Tell me if you can't retrieve.
>>>>
>>>> On Mon, May 17, 2010 at 3:06 PM, <[hidden email]>
>>> 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:[hidden email]]
>>>>> Sent: Sunday, May 16, 2010 8:54 PM
>>>>> To: [hidden email]
>>>>> 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: [hidden email]
>>>>> For additional commands, e-mail: [hidden email]
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> 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: [hidden email]
>>>>> For additional commands, e-mail: [hidden email]
>>>>>
>>>>>
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28580194.html
>>> Sent from the POI - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Need-help-in-updating-footer-of-Word-Document-tp28566396p28585705.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
Starting to wilt a little now and the old eyes are refusing to focus properly so I will not be able to take this any further tonight but I have managed to make some real progress on the footer thing using XWPF.

As the basis for my experiments I used the test code - which I strongly suspect was put together by Yegor - and managed to come up with some code that will insert a number of different items of text into a footer. The first wrinkle I found was that I needed to create a blank document using Word itself and to insert a blank footer into that document (I also removed all of the defaulted in text from the footer). Next, I used this bit of code to open the document and insert three items of text into the footer;

    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 {
            file = new File(documentFolder, documentName);
            fis = new FileInputStream(file);

            document = new XWPFDocument(fis);

            hfPolicy = document.getHeaderFooterPolicy();

            footer = hfPolicy.getFooter(1);

            footerParas = footer.getParagraphs();

            para = footerParas[0];

            CTP ctP1 = para.getCTP();

            CTR ctR1 = ctP1.addNewR();
            CTText t = ctR1.addNewT();
            t.setStringValue("Paragraph in footer");

            CTR ctR2 = ctP1.addNewR();
            CTText t2 = ctR2.addNewT();
            t2.setStringValue("Another Run in Paragraph in footer");
           
            CTR ctR3 = ctP1.addNewR();
            CTText t3 = ctR3.addNewT();
            t3.setStringValue("Yet another Run in Paragraph in footer");

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

            hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);

            documentName = "Populated Footer " + documentName;

            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("Localised Message: " + ex.getLocalizedMessage());
            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
                }
            }
        }
    }

I need to do a lot more digging to find out how to insert new items of text into the footer and preserve what is already there but this will have to wait until the 'morrow I am afraid. Also, there are lots of features of the code that I am not at all clear on yet but suspect they will come into focus better as I experiment more. Anyway, that is all for tonight as I am off to behave like a slob and watch a documentary about the hospital that was constructed by the Royal Navy in the 1740's somewhere near Portsmouth, oh and enjoy a cup of tea of course.

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:markbrdsly@tiscali.co.uk]
Sent: Sunday, May 16, 2010 8:54 PM
To: user@poi.apache.org
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org




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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
I have not forgotten about this thread but have simply been too busy with 'real' work to spend any time at the PC. Tomorrow is my 'office' day and so I will have the time to play with some more code and will post again as soon as I find out anything else.

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
In reply to this post by madhusudan.reddy
Made some real progress with this over the past few days. It has entailed a small change to the API - to the XWPFHeaderFooterPolicy class - which I hope to submit for review soon. Once I know whether the work has been accepted and included into the API, I will post examples that show how to insert and modify headers and footers.

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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Pavan-9
In reply to this post by Shamal Karunarathne
Shamal Karunarathne <shamalk <at> gmail.com> writes:

>
> There's HeaderStories in HWPF. You can extract headers and footers using
> that. It's clean and clear. Tell me if you can't retrieve.
I am able to get the string content of the headers and that works fine. However,
the header also contains images. How do I get the images?.
I have tried using HWPFDocument.getHeaderStoryRange(), but as the documentation
says, it will return the headers,  footer, end note separators and footnote
separators. I am interested in onlt the header contents. Is there is way to do
this?.
Thanks,
-Archita.


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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Paul C.
In reply to this post by Mark Beardsley
Hi Mark, I am trying to figure out exactly the same issue as to how to insert a cutomized footer to word doc using poi. The google search brought me here. I wonder if there has been any progress being made. I tried out your previous examples with POI3.9. I still can not insert a footer. Any insight?
Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Need help in updating footer of Word Document

Mark Beardsley
Take a look at this thread - http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-td5711976.html - a lot of work was undertaken there to work with headers and footers and it might be of help as both are managed by the same objetcs. I no longer actively use the API - we use Open/LibreOffice exclusively now - so my detailed knowledge is a way out of date making it impossible, and unwise, for me to comment any further on this topic.

Yours

Mark B
Loading...