简体   繁体   English

如何使用docx4j将漂亮的JSON打印到Word文档中?

[英]How to print pretty JSON using docx4j into a word document?

I want to print a simple pretty json string (containing multiple line breaks - many \\n) into a word document. 我想在Word文档中打印一个简单的漂亮json字符串(包含多个换行符-许多\\ n)。 I tried the following but docx4j just prints all the contents inline in one single line (without \\n). 我尝试了以下操作,但是docx4j仅将所有内容内联打印在一行中(不带\\ n)。 Ideally it should print multiline pretty json as it is recognising the "\\n" the json string contains : 理想情况下,它应该打印多行漂亮的json,因为它可以识别json字符串包含的“ \\ n”:

1) 1)

  wordMLPackage.getMainDocumentPart().addParagraphOfText({multiline pretty json String})

2) 2)

  ObjectFactory factory = Context.getWmlObjectFactory();
  P p = factory.createP();
  Text t = factory.createText();
  t.setValue(text);
  R run = factory.createR();
  run.getContent().add(t);
  p.getContent().add(run);
  PPr ppr = factory.createPPr();
  p.setPPr(ppr);
  ParaRPr paraRpr = factory.createParaRPr();
  ppr.setRPr(paraRpr);
  wordMLPackage.getMainDocumentPart().addObject(p);

Looking for help. 寻求帮助。 Thanks. 谢谢。

The docx file format doesn't treat \\n as a newline. docx文件格式不会将\\ n视为换行符。

So you'll need to split your string on \\n, and either create a new P, or use w:br, like so: 因此,您需要在\\ n上拆分字符串,然后创建一个新的P或使用w:br,如下所示:

Br br = wmlObjectFactory.createBr(); 
run.getContent().add( br);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM