Home > Blockchain >  How to remove a line break when creating a text paragraph with apache POI
How to remove a line break when creating a text paragraph with apache POI

Time:06-01

I'm working on a project that aims to create automated PPTX. Everything is going fine, except that when creating a XSLFTextBox, a line break appear at the beginning of the box. 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.

CodePudding user response:

Looks like there is already a paragraph in the textbox after creating it with 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. That's annoying, but can't be changed. Using the showed check-routine should fit it all.

  • Related