You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLSlideShow.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.usermodel;
  16. import static org.apache.logging.log4j.util.Unbox.box;
  17. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  18. import java.awt.Dimension;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.OptionalLong;
  31. import java.util.regex.Pattern;
  32. import java.util.stream.Stream;
  33. import org.apache.logging.log4j.LogManager;
  34. import org.apache.logging.log4j.Logger;
  35. import org.apache.poi.ooxml.POIXMLDocument;
  36. import org.apache.poi.ooxml.POIXMLDocumentPart;
  37. import org.apache.poi.ooxml.POIXMLException;
  38. import org.apache.poi.ooxml.extractor.POIXMLPropertiesTextExtractor;
  39. import org.apache.poi.ooxml.util.PackageHelper;
  40. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  41. import org.apache.poi.openxml4j.opc.OPCPackage;
  42. import org.apache.poi.openxml4j.opc.PackagePart;
  43. import org.apache.poi.sl.usermodel.MasterSheet;
  44. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  45. import org.apache.poi.sl.usermodel.SlideShow;
  46. import org.apache.poi.util.Beta;
  47. import org.apache.poi.util.IOUtils;
  48. import org.apache.poi.util.Internal;
  49. import org.apache.poi.util.LittleEndian;
  50. import org.apache.poi.util.LittleEndianConsts;
  51. import org.apache.poi.util.Units;
  52. import org.apache.xmlbeans.XmlException;
  53. import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMasterIdList;
  54. import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMasterIdListEntry;
  55. import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
  56. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
  57. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
  58. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideSize;
  59. import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
  60. /**
  61. * High level representation of an ooxml slideshow.
  62. * This is the first object most users will construct whether
  63. * they are reading or writing a slideshow. It is also the
  64. * top level object for creating new slides/etc.
  65. */
  66. @SuppressWarnings("WeakerAccess")
  67. @Beta
  68. public class XMLSlideShow extends POIXMLDocument
  69. implements SlideShow<XSLFShape, XSLFTextParagraph> {
  70. private static final Logger LOG = LogManager.getLogger(XMLSlideShow.class);
  71. //arbitrarily selected; may need to increase
  72. private static final int DEFAULT_MAX_RECORD_LENGTH = 1_000_000;
  73. private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;
  74. private CTPresentation _presentation;
  75. private final List<XSLFSlide> _slides = new ArrayList<>();
  76. private final List<XSLFSlideMaster> _masters = new ArrayList<>();
  77. private final List<XSLFPictureData> _pictures = new ArrayList<>();
  78. private final List<XSLFChart> _charts = new ArrayList<>();
  79. private XSLFTableStyles _tableStyles;
  80. private XSLFNotesMaster _notesMaster;
  81. private XSLFCommentAuthors _commentAuthors;
  82. /**
  83. * @param length the max record length allowed for XMLSlideShow
  84. */
  85. public static void setMaxRecordLength(int length) {
  86. MAX_RECORD_LENGTH = length;
  87. }
  88. /**
  89. * @return the max record length allowed for XMLSlideShow
  90. */
  91. public static int getMaxRecordLength() {
  92. return MAX_RECORD_LENGTH;
  93. }
  94. public XMLSlideShow() {
  95. this(empty());
  96. }
  97. /**
  98. * @param pkg OPC package
  99. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  100. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  101. * input format
  102. */
  103. public XMLSlideShow(OPCPackage pkg) {
  104. super(pkg);
  105. try {
  106. if (getCorePart().getContentType().equals(XSLFRelation.THEME_MANAGER.getContentType())) {
  107. rebase(getPackage());
  108. }
  109. //build a tree of POIXMLDocumentParts, this presentation being the root
  110. load(XSLFFactory.getInstance());
  111. } catch (Exception e) {
  112. throw new POIXMLException(e);
  113. }
  114. }
  115. /**
  116. * @param is InputStream
  117. * @throws IOException
  118. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  119. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  120. * input format
  121. */
  122. public XMLSlideShow(InputStream is) throws IOException {
  123. this(PackageHelper.open(is));
  124. }
  125. static OPCPackage empty() {
  126. InputStream is = XMLSlideShow.class.getResourceAsStream("empty.pptx");
  127. if (is == null) {
  128. throw new POIXMLException("Missing resource 'empty.pptx'");
  129. }
  130. try {
  131. return OPCPackage.open(is);
  132. } catch (Exception e) {
  133. throw new POIXMLException(e);
  134. } finally {
  135. IOUtils.closeQuietly(is);
  136. }
  137. }
  138. @Override
  139. protected void onDocumentRead() throws IOException {
  140. try {
  141. try (InputStream stream = getCorePart().getInputStream()) {
  142. PresentationDocument doc = PresentationDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  143. _presentation = doc.getPresentation();
  144. }
  145. Map<String, XSLFSlideMaster> masterMap = new HashMap<>();
  146. Map<String, XSLFSlide> shIdMap = new HashMap<>();
  147. Map<String, XSLFChart> chartMap = new HashMap<>();
  148. for (RelationPart rp : getRelationParts()) {
  149. POIXMLDocumentPart p = rp.getDocumentPart();
  150. if (p instanceof XSLFSlide) {
  151. shIdMap.put(rp.getRelationship().getId(), (XSLFSlide) p);
  152. for (POIXMLDocumentPart c : p.getRelations()) {
  153. if (c instanceof XSLFChart) {
  154. chartMap.put(c.getPackagePart().getPartName().getName(), (XSLFChart) c);
  155. }
  156. }
  157. } else if (p instanceof XSLFSlideMaster) {
  158. masterMap.put(getRelationId(p), (XSLFSlideMaster) p);
  159. } else if (p instanceof XSLFTableStyles) {
  160. _tableStyles = (XSLFTableStyles) p;
  161. } else if (p instanceof XSLFNotesMaster) {
  162. _notesMaster = (XSLFNotesMaster) p;
  163. } else if (p instanceof XSLFCommentAuthors) {
  164. _commentAuthors = (XSLFCommentAuthors) p;
  165. }
  166. }
  167. _charts.clear();
  168. _charts.addAll(chartMap.values());
  169. _masters.clear();
  170. if (_presentation.isSetSldMasterIdLst()) {
  171. _presentation.getSldMasterIdLst().getSldMasterIdList().forEach(
  172. id -> _masters.add(masterMap.get(id.getId2()))
  173. );
  174. }
  175. _slides.clear();
  176. if (_presentation.isSetSldIdLst()) {
  177. _presentation.getSldIdLst().getSldIdList().forEach(id -> {
  178. XSLFSlide sh = shIdMap.get(id.getId2());
  179. if (sh == null) {
  180. LOG.atWarn().log("Slide with r:id {} was defined, but didn't exist in package, skipping", box(id.getId()));
  181. } else {
  182. _slides.add(sh);
  183. }
  184. });
  185. }
  186. } catch (XmlException e) {
  187. throw new POIXMLException(e);
  188. }
  189. }
  190. @Override
  191. protected void commit() throws IOException {
  192. PackagePart part = getPackagePart();
  193. try (OutputStream out = part.getOutputStream()) {
  194. _presentation.save(out, DEFAULT_XML_OPTIONS);
  195. }
  196. }
  197. /**
  198. * Get the document's embedded files.
  199. */
  200. @Override
  201. public List<PackagePart> getAllEmbeddedParts() {
  202. return Collections.unmodifiableList(
  203. getPackage().getPartsByName(Pattern.compile("/ppt/embeddings/.*?"))
  204. );
  205. }
  206. @Override
  207. public List<XSLFPictureData> getPictureData() {
  208. if (_pictures.isEmpty()) {
  209. getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?")).forEach(part -> {
  210. XSLFPictureData pd = new XSLFPictureData(part);
  211. pd.setIndex(_pictures.size());
  212. _pictures.add(pd);
  213. });
  214. }
  215. return Collections.unmodifiableList(_pictures);
  216. }
  217. /**
  218. * Create a slide and initialize it from the specified layout.
  219. *
  220. * @param layout The layout to use for the new slide.
  221. * @return created slide
  222. */
  223. public XSLFSlide createSlide(XSLFSlideLayout layout) {
  224. CTSlideIdList slideList = _presentation.isSetSldIdLst()
  225. ? _presentation.getSldIdLst() : _presentation.addNewSldIdLst();
  226. @SuppressWarnings("deprecation")
  227. OptionalLong maxId = Stream.of(slideList.getSldIdArray())
  228. .mapToLong(CTSlideIdListEntry::getId).max();
  229. final XSLFRelation relationType = XSLFRelation.SLIDE;
  230. final int slideNumber = (int)(Math.max(maxId.orElse(0),255)+1);
  231. final int cnt = findNextAvailableFileNameIndex(relationType);
  232. RelationPart rp = createRelationship
  233. (relationType, XSLFFactory.getInstance(), cnt, false);
  234. XSLFSlide slide = rp.getDocumentPart();
  235. CTSlideIdListEntry slideId = slideList.addNewSldId();
  236. slideId.setId(slideNumber);
  237. slideId.setId2(rp.getRelationship().getId());
  238. layout.copyLayout(slide);
  239. slide.getPackagePart().clearRelationships();
  240. slide.addRelation(null, XSLFRelation.SLIDE_LAYOUT, layout);
  241. _slides.add(slide);
  242. return slide;
  243. }
  244. private int findNextAvailableFileNameIndex(XSLFRelation relationType) {
  245. // Bug 55791: We also need to check that the resulting file name is not already taken
  246. // this can happen when removing/adding slides, notes or charts
  247. try {
  248. return getPackage().getUnusedPartIndex(relationType.getDefaultFileName());
  249. } catch (InvalidFormatException e) {
  250. throw new RuntimeException(e);
  251. }
  252. }
  253. /**
  254. * Create a blank slide using the default (first) master.
  255. */
  256. @Override
  257. public XSLFSlide createSlide() {
  258. XSLFSlideMaster sm = _masters.get(0);
  259. XSLFSlideLayout layout = sm.getLayout(SlideLayout.BLANK);
  260. if (layout == null) {
  261. LOG.atWarn().log("Blank layout was not found - defaulting to first slide layout in master");
  262. XSLFSlideLayout[] sl = sm.getSlideLayouts();
  263. if (sl.length == 0) {
  264. throw new POIXMLException("SlideMaster must contain a SlideLayout.");
  265. }
  266. layout = sl[0];
  267. }
  268. return createSlide(layout);
  269. }
  270. /**
  271. * Create a blank chart on the given slide.
  272. */
  273. public XSLFChart createChart(XSLFSlide slide) {
  274. XSLFChart chart = createChart();
  275. slide.addRelation(null, XSLFRelation.CHART, chart);
  276. return chart;
  277. }
  278. /**
  279. * This method is used to create template for chart XML.
  280. * @return Xslf chart object
  281. * @since POI 4.1.0
  282. */
  283. public XSLFChart createChart() {
  284. int chartIdx = findNextAvailableFileNameIndex(XSLFRelation.CHART);
  285. XSLFChart chart = createRelationship(XSLFRelation.CHART, XSLFFactory.getInstance(), chartIdx, true).getDocumentPart();
  286. chart.setChartIndex(chartIdx);
  287. _charts.add(chart);
  288. return chart;
  289. }
  290. /**
  291. * Return notes slide for the specified slide or create new if it does not exist yet.
  292. */
  293. public XSLFNotes getNotesSlide(XSLFSlide slide) {
  294. XSLFNotes notesSlide = slide.getNotes();
  295. if (notesSlide == null) {
  296. notesSlide = createNotesSlide(slide);
  297. }
  298. return notesSlide;
  299. }
  300. /**
  301. * Create a blank notes slide.
  302. */
  303. private XSLFNotes createNotesSlide(XSLFSlide slide) {
  304. if (_notesMaster == null) {
  305. createNotesMaster();
  306. }
  307. XSLFRelation relationType = XSLFRelation.NOTES;
  308. int slideIndex = findNextAvailableFileNameIndex(relationType);
  309. // add notes slide to presentation
  310. XSLFNotes notesSlide = (XSLFNotes) createRelationship
  311. (relationType, XSLFFactory.getInstance(), slideIndex);
  312. // link slide and notes slide with each other
  313. slide.addRelation(null, relationType, notesSlide);
  314. notesSlide.addRelation(null, XSLFRelation.NOTES_MASTER, _notesMaster);
  315. notesSlide.addRelation(null, XSLFRelation.SLIDE, slide);
  316. notesSlide.importContent(_notesMaster);
  317. return notesSlide;
  318. }
  319. /**
  320. * Create a notes master.
  321. */
  322. public void createNotesMaster() {
  323. RelationPart rp = createRelationship
  324. (XSLFRelation.NOTES_MASTER, XSLFFactory.getInstance(), 1, false);
  325. _notesMaster = rp.getDocumentPart();
  326. CTNotesMasterIdList notesMasterIdList = _presentation.addNewNotesMasterIdLst();
  327. CTNotesMasterIdListEntry notesMasterId = notesMasterIdList.addNewNotesMasterId();
  328. notesMasterId.setId(rp.getRelationship().getId());
  329. int themeIndex = 1;
  330. // TODO: check if that list can be replaced by idx = Math.max(idx,themeIdx)
  331. List<Integer> themeIndexList = new ArrayList<>();
  332. for (POIXMLDocumentPart p : getRelations()) {
  333. if (p instanceof XSLFTheme) {
  334. themeIndexList.add(XSLFRelation.THEME.getFileNameIndex(p));
  335. }
  336. }
  337. if (!themeIndexList.isEmpty()) {
  338. boolean found = false;
  339. for (int i = 1; i <= themeIndexList.size(); i++) {
  340. if (!themeIndexList.contains(i)) {
  341. found = true;
  342. themeIndex = i;
  343. }
  344. }
  345. if (!found) {
  346. themeIndex = themeIndexList.size() + 1;
  347. }
  348. }
  349. XSLFTheme theme = (XSLFTheme) createRelationship
  350. (XSLFRelation.THEME, XSLFFactory.getInstance(), themeIndex);
  351. theme.importTheme(getSlides().get(0).getTheme());
  352. _notesMaster.addRelation(null, XSLFRelation.THEME, theme);
  353. }
  354. /**
  355. * Return the Notes Master, if there is one.
  356. * (May not be present if no notes exist)
  357. */
  358. public XSLFNotesMaster getNotesMaster() {
  359. return _notesMaster;
  360. }
  361. @Override
  362. public List<XSLFSlideMaster> getSlideMasters() {
  363. return _masters;
  364. }
  365. /**
  366. * Return all the slides in the slideshow
  367. */
  368. @Override
  369. public List<XSLFSlide> getSlides() {
  370. return _slides;
  371. }
  372. /**
  373. * Return all the charts in the slideshow
  374. */
  375. public List<XSLFChart> getCharts() {
  376. return Collections.unmodifiableList(_charts);
  377. }
  378. /**
  379. * Returns the list of comment authors, if there is one.
  380. * Will only be present if at least one slide has comments on it.
  381. */
  382. public XSLFCommentAuthors getCommentAuthors() {
  383. return _commentAuthors;
  384. }
  385. /**
  386. * @param newIndex 0-based index of the slide
  387. */
  388. public void setSlideOrder(XSLFSlide slide, int newIndex) {
  389. int oldIndex = _slides.indexOf(slide);
  390. if (oldIndex == -1) {
  391. throw new IllegalArgumentException("Slide not found");
  392. }
  393. if (oldIndex == newIndex) {
  394. return;
  395. }
  396. // fix the usermodel container
  397. _slides.add(newIndex, _slides.remove(oldIndex));
  398. // fix ordering in the low-level xml
  399. CTSlideIdList sldIdLst = _presentation.getSldIdLst();
  400. @SuppressWarnings("deprecation")
  401. CTSlideIdListEntry[] entries = sldIdLst.getSldIdArray();
  402. CTSlideIdListEntry oldEntry = entries[oldIndex];
  403. if (oldIndex < newIndex) {
  404. System.arraycopy(entries, oldIndex + 1, entries, oldIndex, newIndex - oldIndex);
  405. } else {
  406. System.arraycopy(entries, newIndex, entries, newIndex + 1, oldIndex - newIndex);
  407. }
  408. entries[newIndex] = oldEntry;
  409. sldIdLst.setSldIdArray(entries);
  410. }
  411. public XSLFSlide removeSlide(int index) {
  412. XSLFSlide slide = _slides.remove(index);
  413. removeRelation(slide);
  414. _presentation.getSldIdLst().removeSldId(index);
  415. for (POIXMLDocumentPart p : slide.getRelations()) {
  416. if (p instanceof XSLFChart) {
  417. XSLFChart chart = (XSLFChart) p;
  418. slide.removeChartRelation(chart);
  419. _charts.remove(chart);
  420. } else if (p instanceof XSLFSlideLayout) {
  421. XSLFSlideLayout layout = (XSLFSlideLayout) p;
  422. slide.removeLayoutRelation(layout);
  423. }
  424. }
  425. return slide;
  426. }
  427. @Override
  428. public Dimension getPageSize() {
  429. CTSlideSize sz = _presentation.getSldSz();
  430. int cx = sz.getCx();
  431. int cy = sz.getCy();
  432. return new Dimension((int) Units.toPoints(cx), (int) Units.toPoints(cy));
  433. }
  434. @Override
  435. public void setPageSize(Dimension pgSize) {
  436. CTSlideSize sz = CTSlideSize.Factory.newInstance();
  437. sz.setCx(Units.toEMU(pgSize.getWidth()));
  438. sz.setCy(Units.toEMU(pgSize.getHeight()));
  439. _presentation.setSldSz(sz);
  440. }
  441. @Internal
  442. public CTPresentation getCTPresentation() {
  443. return _presentation;
  444. }
  445. /**
  446. * Adds a picture to the workbook.
  447. *
  448. * @param pictureData The bytes of the picture
  449. * @param format The format of the picture.
  450. * @return the picture data
  451. */
  452. @Override
  453. public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
  454. XSLFPictureData img = findPictureData(pictureData);
  455. if (img != null) {
  456. return img;
  457. }
  458. XSLFRelation relType = XSLFPictureData.getRelationForType(format);
  459. if (relType == null) {
  460. throw new IllegalArgumentException("Picture type " + format + " is not supported.");
  461. }
  462. int imageNumber;
  463. try {
  464. imageNumber = getPackage().getUnusedPartIndex("/ppt/media/image#\\..+");
  465. } catch (InvalidFormatException e) {
  466. imageNumber = _pictures.size() + 1;
  467. }
  468. img = createRelationship(relType, XSLFFactory.getInstance(), imageNumber, true).getDocumentPart();
  469. img.setIndex(_pictures.size());
  470. _pictures.add(img);
  471. try (OutputStream out = img.getPackagePart().getOutputStream()) {
  472. out.write(pictureData);
  473. } catch (IOException e) {
  474. throw new POIXMLException(e);
  475. }
  476. return img;
  477. }
  478. /**
  479. * Adds a picture to the slideshow.
  480. *
  481. * @param is The stream to read image from
  482. * @param format The format of the picture
  483. * @return the picture data
  484. * @since 3.15 beta 2
  485. */
  486. @Override
  487. public XSLFPictureData addPicture(InputStream is, PictureType format) throws IOException {
  488. return addPicture(IOUtils.toByteArrayWithMaxLength(is, XSLFPictureData.getMaxImageSize()), format);
  489. }
  490. /**
  491. * Adds a picture to the presentation.
  492. *
  493. * @param pict The file containing the image to add
  494. * @param format The format of the picture.
  495. * @return the picture data
  496. * @since 3.15 beta 2
  497. */
  498. @Override
  499. public XSLFPictureData addPicture(File pict, PictureType format) throws IOException {
  500. byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH);
  501. try (InputStream is = new FileInputStream(pict)) {
  502. IOUtils.readFully(is, data);
  503. }
  504. return addPicture(data, format);
  505. }
  506. /**
  507. * check if a picture with this picture data already exists in this presentation
  508. *
  509. * @param pictureData The picture data to find in the SlideShow
  510. * @return {@code null} if picture data is not found in this slideshow
  511. * @since 3.15 beta 2
  512. */
  513. @Override
  514. public XSLFPictureData findPictureData(byte[] pictureData) {
  515. long checksum = IOUtils.calculateChecksum(pictureData);
  516. byte[] cs = new byte[LittleEndianConsts.LONG_SIZE];
  517. LittleEndian.putLong(cs, 0, checksum);
  518. for (XSLFPictureData pic : getPictureData()) {
  519. if (Arrays.equals(pic.getChecksum(), cs)) {
  520. return pic;
  521. }
  522. }
  523. return null;
  524. }
  525. /**
  526. * Scan the master slides for the first slide layout with the given name.
  527. *
  528. * @param name The layout name (case-insensitive). Cannot be null.
  529. * @return the first layout found or null on failure
  530. */
  531. public XSLFSlideLayout findLayout(String name) {
  532. for (XSLFSlideMaster master : getSlideMasters()) {
  533. XSLFSlideLayout layout = master.getLayout(name);
  534. if (layout != null) {
  535. return layout;
  536. }
  537. }
  538. return null;
  539. }
  540. public XSLFTableStyles getTableStyles() {
  541. return _tableStyles;
  542. }
  543. @SuppressWarnings("RedundantThrows")
  544. @Override
  545. public MasterSheet<XSLFShape, XSLFTextParagraph> createMasterSheet() throws IOException {
  546. // TODO: implement!
  547. throw new UnsupportedOperationException();
  548. }
  549. @Override
  550. public POIXMLPropertiesTextExtractor getMetadataTextExtractor() {
  551. return new POIXMLPropertiesTextExtractor(this);
  552. }
  553. @Override
  554. public Object getPersistDocument() {
  555. return this;
  556. }
  557. @Override
  558. public XSLFFontInfo addFont(InputStream fontStream) throws IOException {
  559. return XSLFFontInfo.addFontToSlideShow(this, fontStream);
  560. }
  561. @Override
  562. public List<XSLFFontInfo> getFonts() {
  563. return XSLFFontInfo.getFonts(this);
  564. }
  565. /**
  566. * Import a picture data from a document part.
  567. *
  568. * @param blipId the ID of the package relationship to retrieve
  569. * @param parent the parent document part containing the data to import
  570. * @param target the target document part to import the data to
  571. * @return the ID of the created relationship
  572. */
  573. String importBlip(String blipId, POIXMLDocumentPart parent, POIXMLDocumentPart target) {
  574. OPCPackage targetPackage = target.getPackagePart().getPackage();
  575. if (targetPackage != getPackage()) {
  576. throw new RuntimeException("the target document part is not a child of this package");
  577. }
  578. final POIXMLDocumentPart docPart = parent.getRelationPartById(blipId).getDocumentPart();
  579. XSLFPictureData parData;
  580. if (docPart instanceof XSLFPictureData) {
  581. parData = (XSLFPictureData)docPart;
  582. } else {
  583. throw new RuntimeException("cannot import blip " + blipId + " - its document part is not XSLFPictureData");
  584. }
  585. final XSLFPictureData pictureData;
  586. if (targetPackage == parent.getPackagePart().getPackage()) {
  587. // handle ref counter correct, if the parent document is the same as this
  588. pictureData = parData;
  589. } else {
  590. pictureData = addPicture(parData.getData(), parData.getType());
  591. }
  592. RelationPart rp = target.addRelation(null, XSLFRelation.IMAGES, pictureData);
  593. return rp.getRelationship().getId();
  594. }
  595. }