Page 1 of 1

New Line and Carriage Return Character become white space

PostPosted: Tue Dec 07, 2010 5:48 pm
by yan2wei3
Hi All,

we docx4j to replace find certain keyword in word document and replace it with other values.
however we find that when the values we replace contain new line or carriage return character
(ascii 10 and ascii 13). when we set with new value, it will automatically replace with white space (ascii 32).

e.g

newValue = 10/10/2010
11/10/2010

but in the word document after replace it will become "10/10/2010 11/10/2010"

we have debug, the newValue is contain the new line character before we set value.
any one having same cases before ?

below is our code :

Code: Select all
   if (((JAXBElement)child).getDeclaredType().getName().equals("org.docx4j.wml.Text"))
            {
               Text t = (Text)((JAXBElement)child).getValue();
               
               for (int paramIndex=0; paramIndex<paramSize; paramIndex++)
               {
                  tradeConfirmDetail = (TradeConfirmDetail) paramList.get(paramIndex);
                  textToReplace = "<" + tradeConfirmDetail.getConfirmColumnName() + ">";
                  if ((startPos = t.getValue().indexOf(textToReplace)) != -1)
                  {
                     newValue = t.getValue().substring(0, startPos) +
                        tradeConfirmDetail.getConfirmColumnValue() +
                        t.getValue().substring(startPos + textToReplace.length(), t.getValue().length());
                     t.setValue(newValue);
                     break;
                  }
               }
            }

Thanks & Regards
Yan

Re: New Line and Carriage Return Character become white space

PostPosted: Tue Dec 07, 2010 8:16 pm
by jason
Hi

There is a difference between working in Word, and working with OpenXML.

It has nothing to do with docx4j, as you will see if you create a docx containing a paragraph such as:

Code: Select all
          <w:p>
            <w:r>
              <w:t xml:space="preserve">This is set to preserve

but what does that mean???</w:t>
            </w:r>
          </w:p>


in a text editor, and open it in Word.

If you want a new line, you need something like:

Code: Select all
          <w:p>
            <w:r>
              <w:t>line 1</w:t>
              <w:br/>
              <w:t>line 2</w:t>
            </w:r>
          </w:p>


or of course separate paragraph (w:p) elements.

hope this helps .. Jason