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.

TestXSLFSlide.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.poi.sl.TestCommonSL.sameColor;
  17. import static org.junit.Assert.assertArrayEquals;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import java.awt.Color;
  23. import java.io.IOException;
  24. import java.util.List;
  25. import org.apache.poi.xslf.XSLFTestDataSamples;
  26. import org.junit.Test;
  27. /**
  28. * @author Yegor Kozlov
  29. */
  30. public class TestXSLFSlide {
  31. @Test
  32. public void testReadShapes() throws IOException {
  33. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  34. List<XSLFSlide> slides = ppt.getSlides();
  35. XSLFSlide slide1 = slides.get(0);
  36. List<XSLFShape> shapes1 = slide1.getShapes();
  37. assertEquals(7, shapes1.size());
  38. assertEquals("TextBox 3", shapes1.get(0).getShapeName());
  39. assertTrue(shapes1.get(0) instanceof XSLFTextBox);
  40. XSLFAutoShape sh0 = (XSLFAutoShape)shapes1.get(0);
  41. assertEquals("Learning PPTX", sh0.getText());
  42. assertEquals("Straight Connector 5", shapes1.get(1).getShapeName());
  43. assertTrue(shapes1.get(1) instanceof XSLFConnectorShape);
  44. assertEquals("Freeform 6", shapes1.get(2).getShapeName());
  45. assertTrue(shapes1.get(2) instanceof XSLFFreeformShape);
  46. XSLFAutoShape sh2 = (XSLFAutoShape)shapes1.get(2);
  47. assertEquals("Cloud", sh2.getText());
  48. assertEquals("Picture 1", shapes1.get(3).getShapeName());
  49. assertTrue(shapes1.get(3) instanceof XSLFPictureShape);
  50. assertEquals("Table 2", shapes1.get(4).getShapeName());
  51. assertTrue(shapes1.get(4) instanceof XSLFGraphicFrame);
  52. assertEquals("Straight Arrow Connector 7", shapes1.get(5).getShapeName());
  53. assertTrue(shapes1.get(5) instanceof XSLFConnectorShape);
  54. assertEquals("Elbow Connector 9", shapes1.get(6).getShapeName());
  55. assertTrue(shapes1.get(6) instanceof XSLFConnectorShape);
  56. // titles on slide2
  57. XSLFSlide slide2 = slides.get(1);
  58. List<XSLFShape> shapes2 = slide2.getShapes();
  59. assertEquals(2, shapes2.size());
  60. assertTrue(shapes2.get(0) instanceof XSLFAutoShape);
  61. assertEquals("PPTX Title", ((XSLFAutoShape)shapes2.get(0)).getText());
  62. assertTrue(shapes2.get(1) instanceof XSLFAutoShape);
  63. assertEquals("Subtitle\nAnd second line", ((XSLFAutoShape)shapes2.get(1)).getText());
  64. // group shape on slide3
  65. XSLFSlide slide3 = slides.get(2);
  66. List<XSLFShape> shapes3 = slide3.getShapes();
  67. assertEquals(1, shapes3.size());
  68. assertTrue(shapes3.get(0) instanceof XSLFGroupShape);
  69. List<XSLFShape> groupShapes = ((XSLFGroupShape)shapes3.get(0)).getShapes();
  70. assertEquals(3, groupShapes.size());
  71. assertTrue(groupShapes.get(0) instanceof XSLFAutoShape);
  72. assertEquals("Rectangle 1", groupShapes.get(0).getShapeName());
  73. assertTrue(groupShapes.get(1) instanceof XSLFAutoShape);
  74. assertEquals("Oval 2", groupShapes.get(1).getShapeName());
  75. assertTrue(groupShapes.get(2) instanceof XSLFAutoShape);
  76. assertEquals("Right Arrow 3", groupShapes.get(2).getShapeName());
  77. XSLFSlide slide4 = slides.get(3);
  78. List<XSLFShape> shapes4 = slide4.getShapes();
  79. assertEquals(1, shapes4.size());
  80. assertTrue(shapes4.get(0) instanceof XSLFTable);
  81. XSLFTable tbl = (XSLFTable)shapes4.get(0);
  82. assertEquals(3, tbl.getNumberOfColumns());
  83. assertEquals(6, tbl.getNumberOfRows());
  84. ppt.close();
  85. }
  86. @Test
  87. public void testCreateSlide() throws IOException {
  88. XMLSlideShow ppt = new XMLSlideShow();
  89. assertEquals(0, ppt.getSlides().size());
  90. XSLFSlide slide = ppt.createSlide();
  91. assertFalse(slide.getFollowMasterGraphics());
  92. slide.setFollowMasterGraphics(false);
  93. assertFalse(slide.getFollowMasterGraphics());
  94. slide.setFollowMasterGraphics(true);
  95. assertTrue(slide.getFollowMasterGraphics());
  96. ppt.close();
  97. }
  98. @Test
  99. public void testImportContent() throws IOException {
  100. XMLSlideShow ppt = new XMLSlideShow();
  101. XMLSlideShow src = XSLFTestDataSamples.openSampleDocument("themes.pptx");
  102. // create a blank slide and import content from the 4th slide of themes.pptx
  103. XSLFSlide slide1 = ppt.createSlide().importContent(src.getSlides().get(3));
  104. List<XSLFShape> shapes1 = slide1.getShapes();
  105. assertEquals(2, shapes1.size());
  106. XSLFTextShape sh1 = (XSLFTextShape)shapes1.get(0);
  107. assertEquals("Austin Theme", sh1.getText());
  108. XSLFTextRun r1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
  109. assertEquals("Century Gothic", r1.getFontFamily());
  110. assertEquals(40.0, r1.getFontSize(), 0);
  111. assertTrue(r1.isBold());
  112. assertTrue(r1.isItalic());
  113. assertTrue(sameColor(new Color(148, 198, 0), r1.getFontColor()));
  114. assertNull(sh1.getFillColor());
  115. assertNull(sh1.getLineColor());
  116. XSLFTextShape sh2 = (XSLFTextShape)shapes1.get(1);
  117. assertEquals(
  118. "Text in a autoshape is white\n" +
  119. "Fill: RGB(148, 198,0)", sh2.getText());
  120. XSLFTextRun r2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
  121. assertEquals("Century Gothic", r2.getFontFamily());
  122. assertEquals(18.0, r2.getFontSize(), 0);
  123. assertFalse(r2.isBold());
  124. assertFalse(r2.isItalic());
  125. assertTrue(sameColor(Color.white, r2.getFontColor()));
  126. assertEquals(new Color(148, 198, 0), sh2.getFillColor());
  127. assertEquals(new Color(148, 198, 0), sh2.getLineColor()); // slightly different from PowerPoint!
  128. // the 5th slide has a picture and a texture fill
  129. XSLFSlide slide2 = ppt.createSlide().importContent(src.getSlides().get(4));
  130. List<XSLFShape> shapes2 = slide2.getShapes();
  131. assertEquals(2, shapes2.size());
  132. XSLFTextShape sh3 = (XSLFTextShape)shapes2.get(0);
  133. assertEquals("This slide overrides master background with a texture fill", sh3.getText());
  134. XSLFTextRun r3 = sh3.getTextParagraphs().get(0).getTextRuns().get(0);
  135. assertEquals("Century Gothic", r3.getFontFamily());
  136. //assertEquals(32.4.0, r3.getFontSize());
  137. assertTrue(r3.isBold());
  138. assertTrue(r3.isItalic());
  139. assertTrue(sameColor(new Color(148, 198, 0), r3.getFontColor()));
  140. assertNull(sh3.getFillColor());
  141. assertNull(sh3.getLineColor());
  142. XSLFPictureShape sh4 = (XSLFPictureShape)shapes2.get(1);
  143. XSLFPictureShape srcPic = (XSLFPictureShape)src.getSlides().get(4).getShapes().get(1);
  144. assertArrayEquals(sh4.getPictureData().getData(), srcPic.getPictureData().getData());
  145. ppt.close();
  146. }
  147. @Test
  148. public void testMergeSlides() throws IOException {
  149. XMLSlideShow ppt = new XMLSlideShow();
  150. String[] pptx = {"shapes.pptx", "themes.pptx", "layouts.pptx", "backgrounds.pptx"};
  151. for(String arg : pptx){
  152. XMLSlideShow src = XSLFTestDataSamples.openSampleDocument(arg);
  153. for(XSLFSlide srcSlide : src.getSlides()){
  154. ppt.createSlide().importContent(srcSlide);
  155. }
  156. }
  157. assertEquals(30, ppt.getSlides().size());
  158. ppt.close();
  159. }
  160. }