java - Docx4j 字符串中的换行符

标签 java string line-breaks 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 时,字符串打印如下:

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

这是我获取字符串的地方:

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

这是我添加所有变量的地方:

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,但我不知道我现在必须做什么,我希望有人能给我一些提示。

这是一个System.out.println(escDesc);:

enter image description here

这是文档中的字符串:

enter image description here 谢谢。

最佳答案

正如您可能已经发现的那样,WordML 使用不同的元素来表示制表符和换行符(软回车)。

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代码:

            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”之类的内容。

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

关于java - Docx4j 字符串中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436211/

相关文章:

php - HTML 表无法识别 mysql 数据库中的换行符

java - 检测草率的点击 Android

python - 删除包含不同单词的行

java - 更换琴弦部件

excel - 将txt文件导入excel会使换行符消失

输入 Enter 键时的 Java 操作,不显示换行符

java - 2D vector 库

java - hibernate 重复项

java - 如何按值复制(深度复制)类型为 List<List<Integer>> 的对象?

regex - 以编程方式查找正则表达式的字符串?