Page 1 of 1

need help with replacing text with page break

PostPosted: Thu Sep 15, 2011 1:29 am
by ugur
Hello,

I'm new using Dox4j, and need some help to jump start using it. I'm trying to insert a page break in place defined by a tag like ${PAGE_BREAK}.

For that I'm using unmarshallFromTemplate like in the sample code :

Code: Select all
            // tag definition
            HashMap<String, String> mappings = new HashMap<String, String>();
            mappings.put("DATE", Calendar.getInstance().getTime().toString());
            mappings.put("PAGE_BREAK", "<w:br w:type=\"page\" />");
           
            // tag replacement
            String doc = XmlUtils.marshaltoString(part.getJaxbElement(), true);
            part.setJaxbElement((Document) XmlUtils.unmarshallFromTemplate(doc, mappings));


The problem is that no page break is inserted, DATE is well replaced. But concerning PAGE_BREAK, say I have in the original docx:

Code: Select all
<w:p>
   <w:r>
      <w:t>${PAGE_BREAK}</w:t>
   </w:r>
</w:p>


I'm lasting after template application with :

Code: Select all
<w:p>
   <w:r>
      <w:t></w:t>
   </w:r>
</w:p>


What is the right way to achieve this functionality. many thanks

Re: need help with replacing text with page break

PostPosted: Tue Sep 20, 2011 10:15 pm
by jason
That won't work, because unmarshallFromTemplate can only replace the contents of some w:t element with some other text, whereas you are looking to end up with:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p>
      <w:r>
        <w:br w:type="page"/>
      </w:r>
    </w:p>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


So you need a different approach.

Three ideas:

1. Try OpenDoPE. Put the page break in a conditional content control.

2. Or, use XPath to find the w:t you wish to replace, then replace it

3. Or use TraversalUtil

See Getting Started for further hints.