Page 1 of 1

Extracting text present in header and footer.

PostPosted: Thu Feb 07, 2013 6:11 pm
by Robb2012
Hello Everyone,
I want to extract text present in header and footer.
As of now, I am doing something like this:

List<SectionWrapper> sectionWrappers = m_WordMLPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers)
{
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
System.out.println("SECTION");
System.out.println("Headers:");
if (hfp.getDefaultHeader()!=null) System.out.println("-default");
HeaderPart hdp = hfp.getDefaultHeader();
System.out.println("Header part content : " + hdp.getContent());
List<Object> hdpList = hdp.getContent();
}
This gives me the content of the header. This returns a list of object, as seen above.
But how do I extract text from this list?

I know one way of extracting text from a paragraph by using getJAXBNodesViaXPath method and setting the xpath to "//w:r", but this method can only be applied on document part of the wordML package.

I have seen the samples present in docx4j, but they explain only about how to create and add a header/footer and not about extracting text from existing header part.

Eventually I want to modify the text present in this header/footer part.

Please, kindly help me in this situation.
Your help is greatly appreciated.
Thank you.

Re: Extracting text present in header and footer.

PostPosted: Thu Feb 07, 2013 8:00 pm
by jason
Extracting text and modifying text are 2 quite different tasks.

If you want a method which works for both, you should use TraversalUtils. You can then do something with each w:t node (either extract or alter).

The other way of extracting text is to use TextUtils.extractText

There are two other standard ways of modifying text.

One is VariableReplace (not really recommended); the other is content control data binding (recommended).

Hope this helps .. Jason