Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Formatting Text on a Slide in a PPT using Java

  • Difficulty Level : Hard
  • Last Updated : 17 Dec, 2020

To Format text on a slide in PowerPoint Presentation using Java, use a Java Library called Apache POI. Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint, and Excel. Use the Apache guide to install the Apache POI libraries for Windows/Linux Systems.


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. Create an empty Presentation Object using XMLSlideShow from the Apache POI package.
  2. Create SlideMaster Object and get first slide using XSLFSlideMaster.
  3. Set the Layout of Slide using XSLFSlideLayout Object.
  4. Create Slide using the Layout.
  5. Get the second title of Slide using XSLFTextShape Object and add Paragraph to it using XSLFTextParagraph Object.
  6. Add Lines to paragraph using XSLFTextRun Object and add formatting attributes.



// Formatting text on a slide in a PPT using java
// importing Apache POI environment packages
import org.apache.poi.xslf.usermodel.*;
public class FormatTextPPT {
    public static void main(String args[])
        throws IOException
        // creating an empty presentation
        XMLSlideShow ppt = new XMLSlideShow();
        // creating the slide master object
        XSLFSlideMaster slideMaster
            = ppt.getSlideMasters().get(0);
        // select a layout from specified slideLayout list
        XSLFSlideLayout slidelayout = slideMaster.getLayout(
        // creating a slide with title and content layout
        XSLFSlide slide = ppt.createSlide(slidelayout);
        // selection of title place holder
        XSLFTextShape title = slide.getPlaceholder(1);
        // clear the existing text in the slide
        // adding new paragraph
        XSLFTextParagraph paragraph
            = title.addNewTextParagraph();
        // formatting line 1
        XSLFTextRun line1 = paragraph.addNewTextRun();
        line1.setText("Formatted Bold");
        // making the text bold
        // moving to the next line
        // formatting line 2
        XSLFTextRun line2 = paragraph.addNewTextRun();
        line2.setText("Formatted with Color");
        // setting color to the text
        // setting font size to the text
        // moving to the next line
        // formatting line 3
        XSLFTextRun line3 = paragraph.addNewTextRun();
        line3.setText("Formatted with Underline");
        // underlining the text
        // setting color to the text
        // moving to the next line
        // formatting line 4
        XSLFTextRun line4 = paragraph.addNewTextRun();
        line4.setText("Text Formatted with Strike");
        // making the text italic
        // setting color to the text
        // strike through the text
        // setting font size to the text
        // moving to the next line
        // getting path of current working directory
        // to create the pdf file in the same directory of
        // the running java program
        String path = System.getProperty("user.dir");
        path += "/FormattedText.pptx";
        // creating a file object with the path specified
        File file = new File(path);
        FileOutputStream out = new FileOutputStream(file);
        // saving the changes to a file
            "PPT with Formatted Text created successfully!");

After execution of the program : 

Output : FormattedText.ppt

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!