- Apache POI PPT - Discussion
- Apache POI PPT - Useful Resources
- Apache POI PPT - Quick Guide
- Apache POI PPT - PPT to Image
- Apache POI PPT - Merging
- Apache POI PPT - Formatting Text
- Apache POI PPT - Reading Shapes
- Apache POI PPT - Creating Hyperlinks
- Apache POI PPT - Images
- Apache POI PPT - Slide Management
- Apache POI PPT - Slide Layouts
- Apache POI PPT - Presentation
- Apache POI PPT - Classes & Methods
- Apache POI PPT - Installation
- Apache POI PPT - Java API Flavors
- Apache POI PPT - Overview
- Apache POI PPT - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache POI PPT - Formatting Text
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 spde layouts as shown below −
//create the empty presentation XMLSpdeShow ppt = new XMLSpdeShow(); //getting the spde master object XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters().get(0); //select a layout from specified pst XSLFSpdeLayout spdelayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT); //creating a spde with title and content layout XSLFSpde spde = ppt.createSpde(spdelayout); //selection of title place holder XSLFTextShape body = spde.getPlaceholder(1); //clear the existing text in the spde body.clearText(); //adding new paragraph XSLFTextParagraph paragraph = body.addNewTextParagraph(); //creating text run object XSLFTextRun run = paragraph.addNewTextRun();
You can set the font size of the text in the presentation using setFontSize().
run.setFontColor(java.awt.Color.red); run.setFontSize(24);
The following code snippet shows how to apply different formatting styles (bold, itapc, underpne, strikeout) to the text in a presentation.
//change the text into bold format run.setBold(true); //change the text it to itapc format run.setItapc(true) // strike through the text run.setStrikethrough(true); //underpne the text run.setUnderpned(true);
To have pne breaks between paragraphs, use addLineBreak() of the XSLFTextParagraph class as shown below −
paragraph.addLineBreak();
Given below is the complete program to format the text using all the above methods −
import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.SpdeLayout; import org.apache.poi.xslf.usermodel.XMLSpdeShow; import org.apache.poi.xslf.usermodel.XSLFSpde; import org.apache.poi.xslf.usermodel.XSLFSpdeLayout; import org.apache.poi.xslf.usermodel.XSLFSpdeMaster; import org.apache.poi.xslf.usermodel.XSLFTextParagraph; import org.apache.poi.xslf.usermodel.XSLFTextRun; import org.apache.poi.xslf.usermodel.XSLFTextShape; pubpc class TextFormating { pubpc static void main(String args[]) throws IOException { //creating an empty presentation XMLSpdeShow ppt = new XMLSpdeShow(); //getting the spde master object XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters().get(0); //select a layout from specified pst XSLFSpdeLayout spdelayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT); //creating a spde with title and content layout XSLFSpde spde = ppt.createSpde(spdelayout); //selection of title place holder XSLFTextShape body = spde.getPlaceholder(1); //clear the existing text in the spde body.clearText(); //adding new paragraph XSLFTextParagraph paragraph = body.addNewTextParagraph(); //formatting pne 1 XSLFTextRun run1 = paragraph.addNewTextRun(); run1.setText("This is a colored pne"); //setting color to the text run1.setFontColor(java.awt.Color.red); //setting font size to the text run1.setFontSize(24.0); //moving to the next pne paragraph.addLineBreak(); //formatting pne 2 XSLFTextRun run2 = paragraph.addNewTextRun(); run2.setText("This is a bold pne"); run2.setFontColor(java.awt.Color.CYAN); //making the text bold run2.setBold(true); paragraph.addLineBreak(); //formatting pne 3 XSLFTextRun run3 = paragraph.addNewTextRun(); run3.setText(" This is a striked pne"); run3.setFontSize(12.0); //making the text itapc run3.setItapc(true); //strike through the text run3.setStrikethrough(true); paragraph.addLineBreak(); //formatting pne 4 XSLFTextRun run4 = paragraph.addNewTextRun(); run4.setText(" This an underpned pne"); run4.setUnderpned(true); //underpning the text paragraph.addLineBreak(); //creating a file object File file = new File(“TextFormat.pptx”); FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); out.close(); } }
Save the above code as TextFormating.java, and then compile and execute it from the command prompt as follows −
$javac TextFormating.java $java TextFormating
It will compile and execute to generate the following output −
Formatting completed successfully
The spde with formatted text appears as follows −
Advertisements