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