Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestXSLFSlide.java 7.3KB

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