Search code examples
javastringline-breaksdocx4j

Docx4j line breaks in a string


I have this String:

Prueba
Lista:
- li1
- li2
- li3
- li4

               Tabulado
               Tabulado                   Tabulado                   Tabulado                   Tabulado
               Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado
               Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado                   Tabulado

but when I prepare my .docx the string is printed like this:

Prueba Listas: - li1 - li2 - li3 - li4 Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado
Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado

Here is where I get the string:

String escDesc = report.getDescription();
if (escDesc.contains("\n")){
   //escDesc = escDesc.replaceAll("\\n", System.getProperty("line.separator"));
   //escDesc  = escDesc.replaceAll("\\n", "<w:br/>");
}
escDesc = StringEscapeUtils.escapeXml(escDesc); 

valuesAdd.put("DESCRIPCION", escDesc);  

And here is where I add all the variebles:

private void replacePlaceholders()
      throws Exception {

  MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

  VariablePrepare.prepare(wordMLPackage);

  documentPart.variableReplace(valuesAdd);

  List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
  String xml = null;

  for (SectionWrapper sw : sectionWrappers) {
    HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();

    if (hfp != null) {

      HeaderPart headerPart = hfp.getDefaultHeader();

      if (headerPart != null) {
          xml = XmlUtils.marshaltoString(headerPart.getJaxbElement());
      }


    Object obj = null;
    try {
      obj = XmlUtils.unmarshallFromTemplate(xml, valuesAdd);
    } catch (JAXBException e) {
      e.printStackTrace();
    }

    // Inject result into docx
    headerPart.setJaxbElement((Hdr) obj);
    }
  }
}

I want to keep the \n but i don't know what I have to do right now I hope that someone can give me a few tips.

This is a System.out.println(escDesc);:

enter image description here

And this is the string in the document:

enter image description here Thanks.


Solution

  • As you've probably figured out, WordML uses different elements for tabs and line breaks (soft returns).

    The XML looks something like:

            <w:r>
                <w:t>Tabulado</w:t>
                <w:tab/>
                <w:t>Tabulado</w:t>
                <w:br/>
                <w:t>Tabulado</w:t>
            </w:r>
    

    and corresponding Java code:

                org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();
    
                // Create object for tab (wrapped in JAXBElement) 
                R.Tab rtab = wmlObjectFactory.createRTab(); 
                JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped = wmlObjectFactory.createRTab(rtab); 
                // Add it to the run... 
                run.getContent().add( rtabWrapped); 
    
                // Create object for br
                Br br = wmlObjectFactory.createBr(); 
    

    That's the basics. You might be able to replace with something like "TabuladoTabulado".

    Alternatively use content control data binding, or import XHTML.