import
org.apache.poi.sl.usermodel.AutoNumberingScheme;
import
org.apache.poi.sl.usermodel.PictureData;
import
org.apache.poi.sl.usermodel.TableCell;
import
org.apache.poi.sl.usermodel.TextParagraph;
import
org.apache.poi.util.IOUtils;
import
org.apache.poi.xslf.usermodel.SlideLayout;
import
org.apache.poi.xslf.usermodel.XMLSlideShow;
import
org.apache.poi.xslf.usermodel.XSLFAutoShape;
import
org.apache.poi.xslf.usermodel.XSLFHyperlink;
import
org.apache.poi.xslf.usermodel.XSLFPictureData;
import
org.apache.poi.xslf.usermodel.XSLFPictureShape;
import
org.apache.poi.xslf.usermodel.XSLFShape;
import
org.apache.poi.xslf.usermodel.XSLFSlide;
import
org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import
org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import
org.apache.poi.xslf.usermodel.XSLFTable;
import
org.apache.poi.xslf.usermodel.XSLFTableCell;
import
org.apache.poi.xslf.usermodel.XSLFTableRow;
import
org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import
org.apache.poi.xslf.usermodel.XSLFTextRun;
import
org.apache.poi.xslf.usermodel.XSLFTextShape;
import
java.awt.*;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.List;
public
class
PowerPointHelper {
/**
* Read an existing presentation
*
* @param fileLocation
* File location of the presentation
* @return instance of {@link XMLSlideShow}
* @throws IOException
*/
public
XMLSlideShow readWithExistingSlideShow(String fileLocation)
throws
IOException {
return
new
XMLSlideShow(
new
FileInputStream(fileLocation));
}
/**
* Create a sample presentation
*
* @param fileLocation
* File location of the presentation
* @throws IOException
*/
public
void
creatingAPresentation(String fileLocation)
throws
IOException {
XMLSlideShow samplePPT =
new
XMLSlideShow();
XSLFSlideMaster defaultSlideMaster = samplePPT.getSlideMasters().get(
0
);
XSLFSlideLayout defaultLayout = defaultSlideMaster.getLayout(SlideLayout.TITLE_ONLY);
XSLFSlide sampleSlide1 = samplePPT.createSlide(defaultLayout);
XSLFTextShape sampleTitle = sampleSlide1.getPlaceholder(
0
);
sampleTitle.clearText();
XSLFTextParagraph paragraph = sampleTitle.addNewTextParagraph();
XSLFTextRun r1 = paragraph.addNewTextRun();
r1.setText(
"GeeksforGeeks"
);
r1.setFontColor(
new
Color(
78
,
147
,
89
));
r1.setFontSize(
48
.);
ClassLoader classLoader = getClass().getClassLoader();
byte
[] imageData = IOUtils.toByteArray(
new
FileInputStream(classLoader.getResource(
"gfglogo.png"
).getFile()));
XSLFPictureData pictureData = samplePPT.addPicture(imageData, PictureData.PictureType.PNG);
XSLFPictureShape picture = sampleSlide1.createPicture(pictureData);
picture.setAnchor(
new
Rectangle(
320
,
230
,
100
,
92
));
defaultLayout = defaultSlideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide sampleSlide2 = samplePPT.createSlide(defaultLayout);
sampleTitle = sampleSlide2.getPlaceholder(
0
);
sampleTitle.clearText();
XSLFTextRun r = sampleTitle.addNewTextParagraph().addNewTextRun();
r.setText(
"GeeksforGeeks"
);
XSLFHyperlink hyperLink = r.createHyperlink();
XSLFTextShape content = sampleSlide2.getPlaceholder(
1
);
content.clearText();
content.addNewTextParagraph().addNewTextRun().setText(
"First paragraph"
);
content.addNewTextParagraph().addNewTextRun().setText(
"Second paragraph"
);
content.addNewTextParagraph().addNewTextRun().setText(
"Third paragraph"
);
defaultLayout = defaultSlideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide sampleSlide3 = samplePPT.createSlide(defaultLayout);
sampleTitle = sampleSlide3.getPlaceholder(
0
);
sampleTitle.clearText();
r = sampleTitle.addNewTextParagraph().addNewTextRun();
r.setText(
"List creations"
);
content = sampleSlide3.getPlaceholder(
1
);
content.clearText();
XSLFTextParagraph paragraph1 = content.addNewTextParagraph();
paragraph1.setIndentLevel(
0
);
paragraph1.setBullet(
true
);
r1 = paragraph1.addNewTextRun();
r1.setText(
"Bullet creations"
);
XSLFTextParagraph paragraph2 = content.addNewTextParagraph();
paragraph2.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight,
1
);
paragraph2.setIndentLevel(
1
);
XSLFTextRun r2 = paragraph2.addNewTextRun();
r2.setText(
"Numbered List Item - 1"
);
XSLFSlide sampleSlide4 = samplePPT.createSlide();
creationOfTable(sampleSlide4);
FileOutputStream outputStream =
new
FileOutputStream(fileLocation);
samplePPT.write(outputStream);
outputStream.close();
samplePPT.close();
}
public
void
deleteSlide(XMLSlideShow pptFile,
int
slideNumber) {
pptFile.removeSlide(slideNumber);
}
public
void
reorderSlide(XMLSlideShow pptFile,
int
slideNumber,
int
newSlideNumber) {
List<XSLFSlide> slideList = pptFile.getSlides();
XSLFSlide secondSlide = slideList.get(slideNumber);
pptFile.setSlideOrder(secondSlide, newSlideNumber);
}
/**
* Retrieve the placeholder inside a slide
*
* @param slide
* The slide
* @return List of placeholder inside a slide
*/
public
List<XSLFShape> retrieveTemplatePlaceholders(XSLFSlide slide) {
List<XSLFShape> placeholders =
new
ArrayList<>();
for
(XSLFShape shape : slide.getShapes()) {
if
(shape
instanceof
XSLFAutoShape) {
placeholders.add(shape);
}
}
return
placeholders;
}
private
void
creationOfTable(XSLFSlide slide) {
XSLFTable table = slide.createTable();
table.setAnchor(
new
Rectangle(
50
,
50
,
450
,
300
));
int
numColumns =
3
;
int
numRows =
5
;
XSLFTableRow headerRow = table.addRow();
headerRow.setHeight(
50
);
for
(
int
i =
0
; i < numColumns; i++) {
XSLFTableCell th = headerRow.addCell();
XSLFTextParagraph p = th.addNewTextParagraph();
p.setTextAlign(TextParagraph.TextAlign.CENTER);
XSLFTextRun r = p.addNewTextRun();
r.setText(
"HeaderPart "
+ (i +
1
));
r.setBold(
true
);
r.setFontColor(Color.white);
th.setFillColor(
new
Color(
79
,
129
,
189
));
th.setBorderWidth(TableCell.BorderEdge.bottom,
2.0
);
th.setBorderColor(TableCell.BorderEdge.bottom, Color.white);
table.setColumnWidth(i,
150
);
}
for
(
int
rownum =
0
; rownum < numRows; rownum++) {
XSLFTableRow tr = table.addRow();
tr.setHeight(
50
);
for
(
int
i =
0
; i < numColumns; i++) {
XSLFTableCell cell = tr.addCell();
XSLFTextParagraph p = cell.addNewTextParagraph();
XSLFTextRun r = p.addNewTextRun();
r.setText(
"Data "
+ (i * rownum +
1
));
if
(rownum %
2
==
0
) {
cell.setFillColor(
new
Color(
208
,
216
,
232
));
}
else
{
cell.setFillColor(
new
Color(
233
,
247
,
244
));
}
}
}
}
}