Search code examples
javaapache-poixslf

Correct Text Alignment when there is a bullet point using apache poi


I am using apache poi xslf to create a text box and then adding a bullet point in it. Issue is that When bullet point is multi line text, it adds like this

-Text analysis, nGram, Naïve Bayes Text Classifier to identify the nature of the conversation, sentiment and risk of complaint being made

in above bullet point conversation should be aligned with word Text in bullet line i.e. text alignment like this

  • Text analysis, nGram, Naïve Bayes Text Classifier to identify the nature of
    the conversation sentiment and risk of complaint being made.

Following is the code

XSLFTextBox textbox = this.slide.createTextBox(); 
textbox.setAnchor(new Rectangle(this.xAxis,this.yAxis,this.width,this.height));  XSLFTextParagraph contentPara = textbox.addNewTextParagraph(); 
XSLFTextRun bullet1TR = contentPara.addNewTextRun();      contentPara.setBullet(true);  
contentPara.setFontAlign(FontAlign.TOP); contentPara.setTextAlign(TextAlign.LEFT);

Any help is appreciated. Thanks.


Solution

  • At first the whole paragraph needs left indented as much as the bullet point shall have space. Then the first row needs to have a hanging indent of the same width, so the first row, which has the bullet point in it, is not indented in sum.

    Example:

    import java.io.FileOutputStream;
    
    import org.apache.poi.xslf.usermodel.*;
    import org.apache.poi.sl.usermodel.*;
    
    import java.awt.Rectangle;
    
    public class CreatePPTXTextBoxBullet {
    
     public static void main(String[] args) throws Exception {
    
      XMLSlideShow slideShow = new XMLSlideShow();
    
      XSLFSlide slide = slideShow.createSlide();
    
      XSLFTextBox textbox = slide.createTextBox(); 
      textbox.setAnchor(new Rectangle(50, 100, 570, 100));
      XSLFTextParagraph paragraph = textbox.addNewTextParagraph(); 
      paragraph.setBullet(true);
      paragraph.setLeftMargin(25.2); //left margin = indent for the text; 25.2 pt = 25.2/72 = 0.35"
      paragraph.setIndent(-25.2);  //hanging indent first row for the bullet point; -25.2 pt, so first row indent is 0.00 in sum
      paragraph.setFontAlign(TextParagraph.FontAlign.TOP);
      paragraph.setTextAlign(TextParagraph.TextAlign.LEFT);
      XSLFTextRun run = paragraph.addNewTextRun();
      run.setText("Text analysis, nGram, Naïve Bayes Text Classifier to identify the nature of the conversation, sentiment and risk of complaint being made");
    
      FileOutputStream out = new FileOutputStream("CreatePPTXTextBoxBullet.pptx");
      slideShow.write(out);
      out.close();
     }
    }