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.

TestXSLFPictureShape.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.junit.Assert.assertArrayEquals;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  30. import org.apache.poi.util.IOUtils;
  31. import org.apache.poi.xslf.XSLFTestDataSamples;
  32. import org.junit.Test;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  34. public class TestXSLFPictureShape {
  35. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  36. @Test
  37. public void testCreate() throws Exception {
  38. XMLSlideShow ppt1 = new XMLSlideShow();
  39. assertEquals(0, ppt1.getPictureData().size());
  40. byte[] data1 = new byte[100];
  41. for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }
  42. XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG);
  43. assertEquals(0, pdata1.getIndex());
  44. assertEquals(1, ppt1.getPictureData().size());
  45. XSLFSlide slide = ppt1.createSlide();
  46. XSLFPictureShape shape1 = slide.createPicture(pdata1);
  47. assertNotNull(shape1.getPictureData());
  48. assertArrayEquals(data1, shape1.getPictureData().getData());
  49. byte[] data2 = new byte[200];
  50. for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }
  51. XSLFPictureData pdata2 = ppt1.addPicture(data2, PictureType.PNG);
  52. XSLFPictureShape shape2 = slide.createPicture(pdata2);
  53. assertNotNull(shape2.getPictureData());
  54. assertEquals(1, pdata2.getIndex());
  55. assertEquals(2, ppt1.getPictureData().size());
  56. assertArrayEquals(data2, shape2.getPictureData().getData());
  57. XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt1);
  58. ppt1.close();
  59. List<XSLFPictureData> pics = ppt2.getPictureData();
  60. assertEquals(2, pics.size());
  61. assertArrayEquals(data1, pics.get(0).getData());
  62. assertArrayEquals(data2, pics.get(1).getData());
  63. List<XSLFShape> shapes = ppt2.getSlides().get(0).getShapes();
  64. assertArrayEquals(data1, ((XSLFPictureShape) shapes.get(0)).getPictureData().getData());
  65. assertArrayEquals(data2, ((XSLFPictureShape) shapes.get(1)).getPictureData().getData());
  66. ppt2.close();
  67. }
  68. @Test
  69. public void testCreateMultiplePictures() throws Exception {
  70. XMLSlideShow ppt1 = new XMLSlideShow();
  71. XSLFSlide slide1 = ppt1.createSlide();
  72. XSLFGroupShape group1 = slide1.createGroup();
  73. int pictureIndex = 0;
  74. // first add 20 images to the slide
  75. for (int i = 0; i < 20; i++, pictureIndex++) {
  76. byte[] data = new byte[]{(byte)pictureIndex};
  77. XSLFPictureData elementData = ppt1.addPicture(data, PictureType.PNG);
  78. assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19
  79. XSLFPictureShape picture = slide1.createPicture(elementData);
  80. // POI saves images as image1.png, image2.png, etc.
  81. String fileName = "image" + (elementData.getIndex()+1) + ".png";
  82. assertEquals(fileName, picture.getPictureData().getFileName());
  83. assertArrayEquals(data, picture.getPictureData().getData());
  84. }
  85. // and then add next 20 images to a group
  86. for (int i = 0; i < 20; i++, pictureIndex++) {
  87. byte[] data = new byte[]{(byte)pictureIndex};
  88. XSLFPictureData elementData = ppt1.addPicture(data, PictureType.PNG);
  89. XSLFPictureShape picture = group1.createPicture(elementData);
  90. // POI saves images as image1.png, image2.png, etc.
  91. assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19
  92. String fileName = "image" + (pictureIndex + 1) + ".png";
  93. assertEquals(fileName, picture.getPictureData().getFileName());
  94. assertArrayEquals(data, picture.getPictureData().getData());
  95. }
  96. // serialize, read back and check that all images are there
  97. XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt1);
  98. ppt1.close();
  99. // pictures keyed by file name
  100. Map<String, XSLFPictureData> pics = new HashMap<String, XSLFPictureData>();
  101. for(XSLFPictureData p : ppt2.getPictureData()){
  102. pics.put(p.getFileName(), p);
  103. }
  104. assertEquals(40, pics.size());
  105. for (int i = 0; i < 40; i++) {
  106. byte[] data1 = new byte[]{(byte)i};
  107. String fileName = "image" + (i + 1) + ".png";
  108. XSLFPictureData data = pics.get(fileName);
  109. assertNotNull(data);
  110. assertEquals(fileName, data.getFileName());
  111. assertArrayEquals(data1, data.getData());
  112. }
  113. ppt2.close();
  114. }
  115. @Test
  116. public void testImageCaching() throws Exception {
  117. XMLSlideShow ppt = new XMLSlideShow();
  118. byte[] img1 = new byte[]{1,2,3};
  119. byte[] img2 = new byte[]{3,4,5};
  120. XSLFPictureData pdata1 = ppt.addPicture(img1, PictureType.PNG);
  121. assertEquals(0, pdata1.getIndex());
  122. assertEquals(0, ppt.addPicture(img1, PictureType.PNG).getIndex());
  123. XSLFPictureData idx2 = ppt.addPicture(img2, PictureType.PNG);
  124. assertEquals(1, idx2.getIndex());
  125. assertEquals(1, ppt.addPicture(img2, PictureType.PNG).getIndex());
  126. XSLFSlide slide1 = ppt.createSlide();
  127. assertNotNull(slide1);
  128. XSLFSlide slide2 = ppt.createSlide();
  129. assertNotNull(slide2);
  130. ppt.close();
  131. }
  132. @Test
  133. public void testMerge() throws Exception {
  134. XMLSlideShow ppt1 = new XMLSlideShow();
  135. byte[] data1 = new byte[100];
  136. XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG);
  137. XSLFSlide slide1 = ppt1.createSlide();
  138. XSLFPictureShape shape1 = slide1.createPicture(pdata1);
  139. CTPicture ctPic1 = (CTPicture)shape1.getXmlObject();
  140. ctPic1.getNvPicPr().getNvPr().addNewCustDataLst().addNewTags().setId("rId99");
  141. XMLSlideShow ppt2 = new XMLSlideShow();
  142. XSLFSlide slide2 = ppt2.createSlide().importContent(slide1);
  143. XSLFPictureShape shape2 = (XSLFPictureShape)slide2.getShapes().get(0);
  144. assertArrayEquals(data1, shape2.getPictureData().getData());
  145. CTPicture ctPic2 = (CTPicture)shape2.getXmlObject();
  146. assertFalse(ctPic2.getNvPicPr().getNvPr().isSetCustDataLst());
  147. ppt1.close();
  148. ppt2.close();
  149. }
  150. @Test
  151. public void bug58663() throws IOException {
  152. InputStream is = _slTests.openResourceAsStream("shapes.pptx");
  153. XMLSlideShow ppt = new XMLSlideShow(is);
  154. is.close();
  155. XSLFSlide slide = ppt.getSlides().get(0);
  156. XSLFPictureShape ps = (XSLFPictureShape)slide.getShapes().get(3);
  157. slide.removeShape(ps);
  158. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  159. ppt.write(bos);
  160. ppt.close();
  161. XMLSlideShow ppt2 = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray()));
  162. assertTrue(ppt2.getPictureData().isEmpty());
  163. ppt2.close();
  164. }
  165. @Test
  166. public void testTiffImageBug59742() throws Exception {
  167. XMLSlideShow slideShow = new XMLSlideShow();
  168. final InputStream tiffStream = _slTests.openResourceAsStream("testtiff.tif");
  169. final byte[] pictureData = IOUtils.toByteArray(tiffStream);
  170. IOUtils.closeQuietly(tiffStream);
  171. XSLFPictureData pic = slideShow.addPicture(pictureData, PictureType.TIFF);
  172. assertEquals("image/tiff", pic.getContentType());
  173. assertEquals("image1.tiff", pic.getFileName());
  174. slideShow.close();
  175. }
  176. }