|
| 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 | +} |
0 commit comments