Page 1 of 1

Carriage return during a replacement

PostPosted: Tue Jan 31, 2012 3:58 am
by Shex_
Hi,

I have a template (docx) and I have to replace keys words to content.
Code: Select all
for (Object key : templateProperties.keySet()) {
   textValue = textValue.replaceAll(key.toString().toUpperCase(),(String) templateProperties.get(key));
}

But during this replacement, a key word must be replace to a string with carriages returns. I would like analyze this string in order to locate the '\n' character and insert into the docx's XML a carriage return (I know this character and others like to '\r\n' or '^l' don't work).
Code: Select all
if(templateProperties.get(key).toString().indexOf("\n")!=-1)
{
   // Insert a carriage return
}

I tried severals methods without success.

Do you have any ideas for my problem ?

Thanks :)
Shex

Re: Carriage return during a replacement

PostPosted: Wed Feb 01, 2012 6:32 pm
by jason
Do you want to end up with multiple paragraphs, or a single paragraph with soft returns (<w:br/>) in it?

The latter looks like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p>
      <w:r>
        <w:t>The cat</w:t>
      </w:r>
      <w:r>
        <w:br/>
        <w:t>sat on</w:t>
      </w:r>
      <w:r>
        <w:br/>
        <w:t>the mat.</w:t>
      </w:r>
    </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


You could roll your own version of unmarshallFromTemplate so that it substitutes a new line character with:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
</w:t>
      </w:r>
      <w:r>
        <w:br/>
        <w:t>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


You might consider the content control data binding approach, which can already cope with multiline data.

Re: Carriage return during a replacement

PostPosted: Wed Feb 01, 2012 11:14 pm
by Shex_
Hi jason,

Thank you for your reply. :)

In order to resolve my problem, I used the xml (document.xml in the docx). We add nodes like to <w:br/> to the specifics areas.

Shex_