Saturday, January 14, 2017

Apache POI PPT - Classes and Methods

In this chapter, we will learn about a few classes and methods under Apache POI API that are crucial to work on PPT files using Java programs.

Presentation

To create and manage a presentation, you have a class called XMLSlideShow in the package org.apache.poi.xslf.usermodel. Given below are some important methods and a constructor of this class.
Class : XMLSlideShow
Package : org.apache.poi.xslf.usermodel
S.No Constructor and Description
1 XMLSlideShow(java.io.InputStream inputStream)
You can instantiate this class by passing an inputstream class object to it.
S.No Methods and Description
1 int addPicture (byte[] pictureData, int format)
Using this method, you can add a picture to a presentation.
2 XSLFSlide createSlide()
Creates a blank slide in a presentation.
3 XSLFSlide createSlide(XSLFSlideLayout layout)
Creates a slide with a given slide layout.
4 java.util.List <XSLFPictureData> getAllPictures()
Returns an array of all the pictures in a presentation.
5 java.awt.Dimension getPageSize()
Using this method, you can get to know the current page size.
6 XSLFSlideMaster[] getSlideMasters()
Returns the array of all the slides in a presentation.
7 XSLFSlide[] getSlides()
Returns all the slides in a presentation.
8 XSLFSlide removeSlide(int index)
Using this method, you can remove a slide from a presentation.
9 void setPageSize(java.awt.Dimension pgSize)
Using this method, you can reset the page size.
10 void setSlideOrder(XSLFSlide slide, int newIndex)
Using this method, you can reorder the slides.

Slide

To create and manage a slide in a presentation, the methods of the XSLFSlide class are used. Some important methods of this class are mentioned below.
Class : XSLFSlide
Package : org.apache.poi.xslf.usermodel
S.No Methods and Description
1 XSLFBackground getBackground()
Returns the XSLFBackground object which can be used to retrieve details like color and anchor of the background of the slide. You can also draw shapes in the slide using this object.
2 XSLFSlideLayout getSlideLayout()
Provides access to the XSLFSlideLayout object of the current slide.
3 XSLFSlideMaster getSlideMaster()
Provides access to the slide master of the current slide.
4 XSLFTheme getTheme()
Returns the XSLFTheme object of the current slide.
5 java.lang.String getTitle()
Returns the title of the current slide.
6 XSLFSlide importContent(XSLFSheet src)
Copies the contents of another slide to this slide.

Slide Master

It is the component of the presentation having different slide layouts. The XSLFSlideMaster class gives you access to it. Mentioned below are some important methods of this class.
Class : XSLFSlideMaster
Package : org.apache.poi.xslf.usermodel
S.No Description
1 XSLFBackground getBackground()
Returns the common background of the slide master.
2 XSLFSlideLayout getLayout(SlideLayout type)
Returns the XSLFSlideLayout object.
3 XSLFSlideLayout[] getSlideLayouts()
Returns all the slide layouts in this slide master.

Slide Layout

The POI library has a class called XSLFSlideLayout, using which you can manage the layouts of a slide.
Class : XSLFSlideLayout
Package : org.apache.poi.xslf.usermodel
S.No Method and Description
1 void copyLayout(XSLFSlide slide)
This method will copy the placeholders from this layout to the given slide.

Text Paragraph

You can write content to the slide using XSLFTextParagraph class. Below mentioned are some important methods of this class.
Class : XSLFTextParagraph
Package : org.apache.poi.xslf.usermodel
S.No Methods and Description
1 XSLFTextRun addLineBreak()
Inserts a line break in a paragraph.
2 XSLFTextRun addNewTextRun()
Adds a new run of text in a paragraph.
3 void setBulletAutoNumber(ListAutoNumber scheme, int startAt)
Applies automatic numbered bullet points to the paragraph.
4 void setIndent(double value)
Sets the indent to the text in the paragraph.
5 void setLeftMargin(double value)
This method is used to add the left margin of the paragraph.
6 void setLineSpacing(double linespacing)
This method is used to set line spacing in the paragraph.
7 void setTextAlign(TextAlign align)
This method is used to set alignment that is to be set to the paragraph.

Text Run

This is the lowest level of text separation within a text body. You have XSLFTextRun class to manage the text run of a paragraph. Below mentioned are some important methods of this class.
Class : XSLFTextParagraph
Package : org.apache.poi.xslf.usermodel
S.No Methods and Description
1 XSLFHyperlink createHyperlink()
Creates a hyperlink in the presentation.
2 XSLFHyperlink getHyperlink()
This method is used to get the hyperlink.
3 java.lang.String getText()
Returns the value of this Text node as a Java string.
4 void setBold(boolean bold)
This method is used to set the text in Bold.
5 void setCharacterSpacing(double spc)
Sets the spacing between characters within a text run.
6 void setFontColor(java.awt.Color color)
Sets the font color of the text.
7 void setFontSize(double fontSize)
Sets the font size of the text.
8 void setItalic(boolean italic)
This method is used to make the paragraph italicized.
9 void setStrikethrough(boolean strike)
This method is used to format a run of text as strikethrough text.
10 void setSubscript(boolean flag)
This method is used to format the text as subscript.
11 void setSuperscript(boolean flag)
This method is used to format the text in this run as superscript.
12 void setText(java.lang.String text)
This method is used to set the text in a run.
13 void setUnderline(boolean underline)
This method is used to underline the text in a text run.

Text shape

In PPT, we have shapes that can hold text within them. We can manage these using XSLFTextShape class. Mentioned below are some important methods of this class.
Class : XSLFTextShape
Package : org.apache.poi.xslf.usermodel
S.No Methods and Description
1 void setPlaceholder(Placeholder placeholder)
Using this method, you can choose a place holder.
2 Placeholder getTextType()
Returns the type of the current placeholder.
3 void clearText()
Clears the text area of the current text shape.
4 XSLFTextParagraph addNewTextParagraph()
Adds a new paragraph run to a shape.
5 void drawContent(java.awt.Graphics2D graphics)
This method allows you to draw any content on the slide.

HyperLink

The POI library has a class called XSLFHyperlink using which you can create a hyperlink in the presentation. Mentioned below are some important methods of this class.
Class : XSLFHyperlink
Package : org.apache.poi.xslf.usermodel
S.No Methods and Description
1 java.net.URI getTargetURL()
Returns the URL existing in a slide of the presentation.
2 void setAddress(java.lang.String address)
This method is used to set address to a URL.
3 void setAddress(XSLFSlide slide)
Sets address to the URL present in a slide of the presentation.

No comments:

Post a Comment