Docx4j换行符串 [英] Docx4j line breaks in a string

查看:1377
本文介绍了Docx4j换行符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串:

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

但是当我准备我的.docx时,字符串打印如下:

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

我想保留\\ \\ n但是我不知道我现在要做什么我希望有人可以给我一些提示。

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.

这是 System.out.println(escDesc);

这是文档中的字符串:


谢谢。

推荐答案

正如您可能已经想到的那样,WordML对标签和换行符使用不同的元素(软返回)。

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

XML类似于:

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

及相应的Java代码:

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

这是基础知识。你可以用TabuladoTabulado这样的东西替换。

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

或者使用内容控制数据绑定,或导入XHTML。

Alternatively use content control data binding, or import XHTML.

这篇关于Docx4j换行符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆