The text in a presentation can be formatted using the methods of the XSLFTextRun class. For that, you have to create an XSLFTextRun class object by selecting one of the slide layouts as shown below −

    1. //create the empty presentation
    2. XMLSlideShow ppt = new XMLSlideShow();
    3.  
    4. //getting the slide master object
    5. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
    6.  
    7. //select a layout from specified list
    8. XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
    9.  
    10. //creating a slide with title and content layout
    11. XSLFSlide slide = ppt.createSlide(slidelayout);
    12.  
    13. //selection of title place holder
    14. XSLFTextShape body = slide.getPlaceholder(1);
    15.  
    16. //clear the existing text in the slide
    17. body.clearText();
    18.  
    19. //adding new paragraph
    20. XSLFTextParagraph paragraph = body.addNewTextParagraph();
    21.  
    22. //creating text run object
    23. XSLFTextRun run = paragraph.addNewTextRun();

    You can set the font size of the text in the presentation using setFontSize().

    1. run.setFontColor(java.awt.Color.red);
    2. run.setFontSize(24);

    The following code snippet shows how to apply different formatting styles (bold, italic, underline, strikeout) to the text in a presentation.

    1. //change the text into bold format
    2. run.setBold(true);
    3.  
    4. //change the text it to italic format
    5. run.setItalic(true)
    6.  
    7. // strike through the text
    8. run.setStrikethrough(true);
    9.  
    10. //underline the text
    11. run.setUnderline(true);

    To have line breaks between paragraphs, use addLineBreak() of the XSLFTextParagraph class as shown below −

    1. paragraph.addLineBreak();

    Given below is the complete program to format the text using all the above methods −

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3.  
    4. import org.apache.poi.xslf.usermodel.SlideLayout;
    5. import org.apache.poi.xslf.usermodel.XMLSlideShow;
    6. import org.apache.poi.xslf.usermodel.XSLFSlide;
    7. import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
    8. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
    9. import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
    10. import org.apache.poi.xslf.usermodel.XSLFTextRun;
    11. import org.apache.poi.xslf.usermodel.XSLFTextShape;
    12.  
    13. public class TextFormating {
    14.  
    15. public static void main(String args[]) throws IOException {
    16.  
    17. //creating an empty presentation
    18. XMLSlideShow ppt = new XMLSlideShow();
    19.  
    20. //getting the slide master object
    21. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
    22.  
    23. //select a layout from specified list
    24. XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
    25.  
    26. //creating a slide with title and content layout
    27. XSLFSlide slide = ppt.createSlide(slidelayout);
    28.  
    29. //selection of title place holder
    30. XSLFTextShape body = slide.getPlaceholder(1);
    31.  
    32. //clear the existing text in the slide
    33. body.clearText();
    34.  
    35. //adding new paragraph
    36. XSLFTextParagraph paragraph = body.addNewTextParagraph();
    37.  
    38. //formatting line 1
    39.  
    40. XSLFTextRun run1 = paragraph.addNewTextRun();
    41. run1.setText(“This is a colored line”);
    42. //setting color to the text
    43. run1.setFontColor(java.awt.Color.red);
    44. //setting font size to the text
    45. run1.setFontSize(24);
    46. //moving to the next line
    47. paragraph.addLineBreak();
    48.  
    49. //formatting line 2
    50.  
    51. XSLFTextRun run2 = paragraph.addNewTextRun();
    52. run2.setText(“This is a bold line”);
    53. run2.setFontColor(java.awt.Color.CYAN);
    54.  
    55. //making the text bold
    56. run2.setBold(true);
    57. paragraph.addLineBreak();
    58.  
    59. //formatting line 3
    60.  
    61. XSLFTextRun run3 = paragraph.addNewTextRun();
    62. run3.setText(“ This is a striked line”);
    63. run3.setFontSize(12);
    64.  
    65. //making the text italic
    66. run3.setItalic(true);
    67.  
    68. //strike through the text
    69. run3.setStrikethrough(true);
    70. paragraph.addLineBreak();
    71.  
    72. //formatting line 4
    73.  
    74. XSLFTextRun run4 = paragraph.addNewTextRun();
    75. run4.setText(“ This an underlined line”);
    76. run4.setUnderline(true);
    77.  
    78. //underlining the text
    79. paragraph.addLineBreak();
    80.  
    81. //creating a file object
    82. File file = new File(“TextFormat.pptx”);
    83. FileOutputStream out = new FileOutputStream(file);
    84.  
    85. //saving the changes to a file
    86. ppt.write(out);
    87. out.close();
      }
    88. }

    Save the above code as TextFormating.java, and then compile and execute it from the command prompt as follows −

    1. $javac TextFormating.java
    2. $java TextFormating

    It will compile and execute to generate the following output −

    1. Formatting completed successfully

    The slide with formatted text appears as follows −

    FormattingText.jpg