简体   繁体   English

使用apache POI创建文本段落时如何删除换行符

[英]How to remove a line break when creating a text paragraph with apache POI

I'm working on a project that aims to create automated PPTX.我正在开发一个旨在创建自动化 PPTX 的项目。 Everything is going fine, except that when creating a XSLFTextBox, a line break appear at the beginning of the box.一切都很好,除了在创建 XSLFTextBox 时,在框的开头出现了换行符。 Here is a part of the code这是代码的一部分

 XSLFTextBox shape = slide.createTextBox();
 XSLFTextParagraph p = shape.addNewTextParagraph();
 XSLFTextRun r1 = p.addNewTextRun();
 r1.setText("Example");

Here is an example of the result这是一个结果示例

I would like to know if there's any method to avoid that behaviour, or if I have to do it another way.我想知道是否有任何方法可以避免这种行为,或者我是否必须以另一种方式来做。 Thanks for reading.谢谢阅读。

Looks like there is already a paragraph in the textbox after creating it with XSLFTextBox textbox = slide.createTextBox();使用 XSLFTextBox textbox XSLFTextBox textbox = slide.createTextBox(); . . So one needs to check this and add a paragraph only if there is not one already,因此,只有在还没有段落时才需要检查并添加段落,

Code:代码:

...
  XSLFTextBox textbox = slide.createTextBox(); 
  textbox.setAnchor(new Rectangle(100, 100, 100, 50));
  XSLFTextParagraph paragraph = null;
  if(textbox.getTextParagraphs().size() > 0) paragraph = textbox.getTextParagraphs().get(0);
  if(paragraph == null) paragraph = textbox.addNewTextParagraph(); 
  XSLFTextRun run = paragraph.addNewTextRun();
  run.setText("Example");
...

Unfortunately one cannot rely on the presence of a paragraph after creating.不幸的是,创建后不能依赖段落的存在。 That may change in apache poi from version to version.这可能会在不同版本的apache poi中发生变化。 That's annoying, but can't be changed.这很烦人,但无法改变。 Using the showed check-routine should fit it all.使用显示的检查例程应该适合所有情况。

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

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