Skip to content

Commit 064ea79

Browse files
araknoidpedja4
authored andcommitted
BAEL-1203 Apache POI PowerPoint (eugenp#3090)
* Added Apache-POI PowerPoint * Adjusted format + retrieve all placeholder
1 parent da97e4c commit 064ea79

2 files changed

Lines changed: 301 additions & 0 deletions

File tree

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package com.baeldung.poi.powerpoint;
2+
3+
import org.apache.poi.sl.usermodel.AutoNumberingScheme;
4+
import org.apache.poi.sl.usermodel.PictureData;
5+
import org.apache.poi.sl.usermodel.TableCell;
6+
import org.apache.poi.sl.usermodel.TextParagraph;
7+
import org.apache.poi.util.IOUtils;
8+
import org.apache.poi.xslf.usermodel.*;
9+
10+
import java.awt.*;
11+
import java.io.FileInputStream;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/**
18+
* Helper class for the PowerPoint presentation creation
19+
*/
20+
public class PowerPointHelper {
21+
22+
/**
23+
* Read an existing presentation
24+
*
25+
* @param fileLocation
26+
* File location of the presentation
27+
* @return instance of {@link XMLSlideShow}
28+
* @throws IOException
29+
*/
30+
public XMLSlideShow readingExistingSlideShow(String fileLocation) throws IOException {
31+
return new XMLSlideShow(new FileInputStream(fileLocation));
32+
}
33+
34+
/**
35+
* Create a sample presentation
36+
*
37+
* @param fileLocation
38+
* File location of the presentation
39+
* @throws IOException
40+
*/
41+
public void createPresentation(String fileLocation) throws IOException {
42+
// Create presentation
43+
XMLSlideShow ppt = new XMLSlideShow();
44+
45+
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
46+
47+
// Retriving the slide layout
48+
XSLFSlideLayout layout = defaultMaster.getLayout(SlideLayout.TITLE_ONLY);
49+
50+
// Creating the 1st slide
51+
XSLFSlide slide1 = ppt.createSlide(layout);
52+
XSLFTextShape title = slide1.getPlaceholder(0);
53+
// Clearing text to remove the predefined one in the template
54+
title.clearText();
55+
XSLFTextParagraph p = title.addNewTextParagraph();
56+
57+
XSLFTextRun r1 = p.addNewTextRun();
58+
r1.setText("Baeldung");
59+
r1.setFontColor(new Color(78, 147, 89));
60+
r1.setFontSize(48.);
61+
62+
// Add Image
63+
ClassLoader classLoader = getClass().getClassLoader();
64+
byte[] pictureData = IOUtils.toByteArray(new FileInputStream(classLoader.getResource("logo-leaf.png").getFile()));
65+
66+
XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.PNG);
67+
XSLFPictureShape picture = slide1.createPicture(pd);
68+
picture.setAnchor(new Rectangle(320, 230, 100, 92));
69+
70+
// Creating 2nd slide
71+
layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
72+
XSLFSlide slide2 = ppt.createSlide(layout);
73+
74+
// setting the tile
75+
title = slide2.getPlaceholder(0);
76+
title.clearText();
77+
XSLFTextRun r = title.addNewTextParagraph().addNewTextRun();
78+
r.setText("Baeldung");
79+
80+
// Adding the link
81+
XSLFHyperlink link = r.createHyperlink();
82+
link.setAddress("http://www.baeldung.com");
83+
84+
// setting the content
85+
XSLFTextShape content = slide2.getPlaceholder(1);
86+
content.clearText(); // unset any existing text
87+
content.addNewTextParagraph().addNewTextRun().setText("First paragraph");
88+
content.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
89+
content.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
90+
91+
// Creating 3rd slide - List
92+
layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
93+
XSLFSlide slide3 = ppt.createSlide(layout);
94+
title = slide3.getPlaceholder(0);
95+
title.clearText();
96+
r = title.addNewTextParagraph().addNewTextRun();
97+
r.setText("Lists");
98+
99+
content = slide3.getPlaceholder(1);
100+
content.clearText();
101+
XSLFTextParagraph p1 = content.addNewTextParagraph();
102+
p1.setIndentLevel(0);
103+
p1.setBullet(true);
104+
r1 = p1.addNewTextRun();
105+
r1.setText("Bullet");
106+
107+
// the next three paragraphs form an auto-numbered list
108+
XSLFTextParagraph p2 = content.addNewTextParagraph();
109+
p2.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 1);
110+
p2.setIndentLevel(1);
111+
XSLFTextRun r2 = p2.addNewTextRun();
112+
r2.setText("Numbered List Item - 1");
113+
114+
// Creating 4th slide
115+
XSLFSlide slide4 = ppt.createSlide();
116+
createTable(slide4);
117+
118+
// Save presentation
119+
FileOutputStream out = new FileOutputStream(fileLocation);
120+
ppt.write(out);
121+
out.close();
122+
123+
// Closing presentation
124+
ppt.close();
125+
}
126+
127+
/**
128+
* Delete a slide from the presentation
129+
*
130+
* @param ppt
131+
* The presentation
132+
* @param slideNumber
133+
* The number of the slide to be deleted (0-based)
134+
*/
135+
public void deleteSlide(XMLSlideShow ppt, int slideNumber) {
136+
ppt.removeSlide(slideNumber);
137+
}
138+
139+
/**
140+
* Re-order the slides inside a presentation
141+
*
142+
* @param ppt
143+
* The presentation
144+
* @param slideNumber
145+
* The number of the slide to move
146+
* @param newSlideNumber
147+
* The new position of the slide (0-base)
148+
*/
149+
public void reorderSlide(XMLSlideShow ppt, int slideNumber, int newSlideNumber) {
150+
List<XSLFSlide> slides = ppt.getSlides();
151+
152+
XSLFSlide secondSlide = slides.get(slideNumber);
153+
ppt.setSlideOrder(secondSlide, newSlideNumber);
154+
}
155+
156+
/**
157+
* Retrieve the placeholder inside a slide
158+
*
159+
* @param slide
160+
* The slide
161+
* @return List of placeholder inside a slide
162+
*/
163+
public List<XSLFShape> retrieveTemplatePlaceholders(XSLFSlide slide) {
164+
List<XSLFShape> placeholders = new ArrayList<>();
165+
166+
for (XSLFShape shape : slide.getShapes()) {
167+
if (shape instanceof XSLFAutoShape) {
168+
placeholders.add(shape);
169+
}
170+
}
171+
return placeholders;
172+
}
173+
174+
/**
175+
* Create a table
176+
*
177+
* @param slide
178+
* Slide
179+
*/
180+
private void createTable(XSLFSlide slide) {
181+
182+
XSLFTable tbl = slide.createTable();
183+
tbl.setAnchor(new Rectangle(50, 50, 450, 300));
184+
185+
int numColumns = 3;
186+
int numRows = 5;
187+
188+
// header
189+
XSLFTableRow headerRow = tbl.addRow();
190+
headerRow.setHeight(50);
191+
for (int i = 0; i < numColumns; i++) {
192+
XSLFTableCell th = headerRow.addCell();
193+
XSLFTextParagraph p = th.addNewTextParagraph();
194+
p.setTextAlign(TextParagraph.TextAlign.CENTER);
195+
XSLFTextRun r = p.addNewTextRun();
196+
r.setText("Header " + (i + 1));
197+
r.setBold(true);
198+
r.setFontColor(Color.white);
199+
th.setFillColor(new Color(79, 129, 189));
200+
th.setBorderWidth(TableCell.BorderEdge.bottom, 2.0);
201+
th.setBorderColor(TableCell.BorderEdge.bottom, Color.white);
202+
// all columns are equally sized
203+
tbl.setColumnWidth(i, 150);
204+
}
205+
206+
// data
207+
for (int rownum = 0; rownum < numRows; rownum++) {
208+
XSLFTableRow tr = tbl.addRow();
209+
tr.setHeight(50);
210+
for (int i = 0; i < numColumns; i++) {
211+
XSLFTableCell cell = tr.addCell();
212+
XSLFTextParagraph p = cell.addNewTextParagraph();
213+
XSLFTextRun r = p.addNewTextRun();
214+
215+
r.setText("Cell " + (i * rownum + 1));
216+
if (rownum % 2 == 0) {
217+
cell.setFillColor(new Color(208, 216, 232));
218+
} else {
219+
cell.setFillColor(new Color(233, 247, 244));
220+
}
221+
}
222+
}
223+
}
224+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.poi.powerpoint;
2+
3+
import org.apache.poi.xslf.usermodel.XMLSlideShow;
4+
import org.apache.poi.xslf.usermodel.XSLFShape;
5+
import org.apache.poi.xslf.usermodel.XSLFSlide;
6+
import org.junit.After;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
import java.util.List;
13+
14+
public class PowerPointIntegrationTest {
15+
16+
private PowerPointHelper pph;
17+
private String fileLocation;
18+
private static final String FILE_NAME = "presentation.pptx";
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
File currDir = new File(".");
23+
String path = currDir.getAbsolutePath();
24+
fileLocation = path.substring(0, path.length() - 1) + FILE_NAME;
25+
26+
pph = new PowerPointHelper();
27+
pph.createPresentation(fileLocation);
28+
}
29+
30+
@Test
31+
public void whenReadingAPresentation_thenOK() throws Exception {
32+
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
33+
34+
Assert.assertNotNull(xmlSlideShow);
35+
Assert.assertEquals(4, xmlSlideShow.getSlides().size());
36+
}
37+
38+
@Test
39+
public void whenRetrievingThePlaceholdersForEachSlide_thenOK() throws Exception {
40+
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
41+
42+
List<XSLFShape> onlyTitleSlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(0));
43+
List<XSLFShape> titleAndBodySlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(1));
44+
List<XSLFShape> emptySlidePlaceholdes = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(3));
45+
46+
Assert.assertEquals(1, onlyTitleSlidePlaceholders.size());
47+
Assert.assertEquals(2, titleAndBodySlidePlaceholders.size());
48+
Assert.assertEquals(0, emptySlidePlaceholdes.size());
49+
50+
}
51+
52+
@Test
53+
public void whenSortingSlides_thenOK() throws Exception {
54+
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
55+
XSLFSlide slide4 = xmlSlideShow.getSlides().get(3);
56+
pph.reorderSlide(xmlSlideShow, 3, 1);
57+
58+
Assert.assertEquals(slide4, xmlSlideShow.getSlides().get(1));
59+
}
60+
61+
@Test
62+
public void givenPresentation_whenDeletingASlide_thenOK() throws Exception {
63+
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
64+
pph.deleteSlide(xmlSlideShow, 3);
65+
66+
Assert.assertEquals(3, xmlSlideShow.getSlides().size());
67+
}
68+
69+
@After
70+
public void tearDown() throws Exception {
71+
File testFile = new File(fileLocation);
72+
if (testFile.exists()) {
73+
testFile.delete();
74+
}
75+
pph = null;
76+
}
77+
}

0 commit comments

Comments
 (0)