Page 1 of 1

parse and change value on a paragraph inside a table

PostPosted: Mon May 23, 2016 6:19 am
by aymenLa
Hello,
i am using the attached template to generate a docx document.
First of all i am replacing the value in side "{}" withreal value.
The second step is the replacement of value inside a Table.
The problem is that i cant detect paragraph when in a Table is.
This tutorial helped me a lot.
http://www.smartjava.org/content/create-complex-word-docx-documents-programatically-docx4j
I can now replace value wi this method :

Code: Select all
MainDocumentPart documentPart = template.getMainDocumentPart();
                HashMap<String, String> mappings = new HashMap<>();
                mappings.put("artnum", "101010");
                mappings.put("bezeichung", "ok es geht");
                mappings.put("farben", "rot blue");
                mappings.put("gros", "L, M ,XL");
                VariablePrepare.prepare(template);


When i try to replace value on a paragraph within a table it wont work.
There is a nested table that i was able to add rows in it. But when i try to add rows with different values on the Global Table it just copy the last values.

Can anyone help !

Re: parse and change value on a paragraph inside a table

PostPosted: Fri Jun 03, 2016 8:02 pm
by jason
Your code snippet doesn't show the VariableReplace code:

Code: Select all
/*      // Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
      
         documentPart.variableReplace(mappings);
*/      
      // Approach 2 (original)
      
         // unmarshallFromTemplate requires string input
         String xml = XmlUtils.marshaltoString(documentPart.getJaxbElement(), true);
         // Do it...
         Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
         // Inject result into docx
         documentPart.setJaxbElement((Document) obj);


Your document contains split runs, for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p w:rsidR="00311F8A" w:rsidRDefault="00311F8A" w:rsidP="00725763">
      <w:r>
        <w:t>${</w:t>
      </w:r>
      <w:proofErr w:type="spellStart"/>
      <w:r w:rsidR="0097049B">
        <w:t>name</w:t>
      </w:r>
      <w:proofErr w:type="spellEnd"/>
      <w:r>
        <w:t>}</w:t>
      </w:r>
    </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


so you definitely need VariablePrepare. VariablePrepare should work there.

aymenLa wrote:There is a nested table that i was able to add rows in it. But when i try to add rows with different values on the Global Table it just copy the last values.


VariableReplace doesn't support creating new table rows.

To repeat table row contents, I recommend OpenDoPE content control databinding. You wrap a repeat content control around the table row, and it will be duplicated automatically for each instance of the data.