Docx4j line breaks in a string

感情迁移 提交于 2019-12-02 18:06:30

问题


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);:

And this is the string in the document:

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/42436211/docx4j-line-breaks-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!