Open In App

Formatting Text on a Slide in a PPT using Java

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Approach:

  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.

Implementation:

Java




// Formatting text on a slide in a PPT using java
import java.io.*;
  
// 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(
            SlideLayout.TITLE_AND_CONTENT);
  
        // 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
        title.clearText();
  
        // adding new paragraph
        XSLFTextParagraph paragraph
            = title.addNewTextParagraph();
  
        // formatting line 1
        XSLFTextRun line1 = paragraph.addNewTextRun();
        line1.setText("Formatted Bold");
        
        // making the text bold
        line1.setBold(true);
        
        // moving to the next line
        paragraph.addLineBreak();
  
        // formatting line 2
        XSLFTextRun line2 = paragraph.addNewTextRun();
        line2.setText("Formatted with Color");
        
        // setting color to the text
        line2.setFontColor(java.awt.Color.RED);
        
        // setting font size to the text
        line2.setFontSize(24.0);
        
        // moving to the next line
        paragraph.addLineBreak();
  
        // formatting line 3
        XSLFTextRun line3 = paragraph.addNewTextRun();
        line3.setText("Formatted with Underline");
        
        // underlining the text
        line3.setUnderlined(true);
        
        // setting color to the text
        line3.setFontColor(java.awt.Color.GRAY);
        
        // moving to the next line
        paragraph.addLineBreak();
  
        // formatting line 4
        XSLFTextRun line4 = paragraph.addNewTextRun();
        line4.setText("Text Formatted with Strike");
        line4.setFontSize(12.0);
        
        // making the text italic
        line4.setItalic(true);
        
        // setting color to the text
        line4.setFontColor(java.awt.Color.BLUE);
        
        // strike through the text
        line4.setStrikethrough(true);
        
        // setting font size to the text
        line4.setFontSize(24.0);
        
        // moving to the next line
        paragraph.addLineBreak();
  
        // 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.write(out);
        out.close();
        ppt.close();
        System.out.println(
            "PPT with Formatted Text created successfully!");
    }
}


After execution of the program : 

Output : FormattedText.ppt



Last Updated : 17 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads