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.

XSLFPictureShape.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import java.awt.Insets;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.io.ByteArrayInputStream;
  24. import java.net.URI;
  25. import javax.imageio.ImageIO;
  26. import javax.xml.namespace.QName;
  27. import org.apache.poi.POIXMLException;
  28. import org.apache.poi.openxml4j.opc.PackagePart;
  29. import org.apache.poi.openxml4j.opc.PackageRelationship;
  30. import org.apache.poi.sl.usermodel.PictureShape;
  31. import org.apache.poi.util.Beta;
  32. import org.apache.xmlbeans.XmlCursor;
  33. import org.apache.xmlbeans.XmlObject;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.*;
  36. /**
  37. * Represents a picture shape
  38. */
  39. @Beta
  40. public class XSLFPictureShape extends XSLFSimpleShape implements PictureShape {
  41. private XSLFPictureData _data;
  42. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  43. super(shape, sheet);
  44. }
  45. /**
  46. * @param shapeId 1-based shapeId
  47. * @param rel relationship to the picture data in the ooxml package
  48. */
  49. static CTPicture prototype(int shapeId, String rel) {
  50. CTPicture ct = CTPicture.Factory.newInstance();
  51. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  52. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  53. cnv.setName("Picture " + shapeId);
  54. cnv.setId(shapeId + 1);
  55. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  56. nvSpPr.addNewNvPr();
  57. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  58. CTBlip blip = blipFill.addNewBlip();
  59. blip.setEmbed(rel);
  60. blipFill.addNewStretch().addNewFillRect();
  61. CTShapeProperties spPr = ct.addNewSpPr();
  62. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  63. prst.setPrst(STShapeType.RECT);
  64. prst.addNewAvLst();
  65. return ct;
  66. }
  67. /**
  68. * Resize this picture to the default size.
  69. * For PNG and JPEG resizes the image to 100%,
  70. * for other types sets the default size of 200x200 pixels.
  71. */
  72. public void resize() {
  73. XSLFPictureData pict = getPictureData();
  74. try {
  75. BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  76. setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
  77. }
  78. catch (Exception e) {
  79. //default size is 200x200
  80. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  81. }
  82. }
  83. /**
  84. * Is this an internal picture (image data included within
  85. * the PowerPoint file), or an external linked picture
  86. * (image lives outside)?
  87. */
  88. public boolean isExternalLinkedPicture() {
  89. if (getBlipId() == null && getBlipLink() != null) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * Return the data on the (internal) picture.
  96. * For an external linked picture, will return null
  97. */
  98. public XSLFPictureData getPictureData() {
  99. if(_data == null){
  100. String blipId = getBlipId();
  101. if (blipId == null) return null;
  102. PackagePart p = getSheet().getPackagePart();
  103. PackageRelationship rel = p.getRelationship(blipId);
  104. if (rel != null) {
  105. try {
  106. PackagePart imgPart = p.getRelatedPart(rel);
  107. _data = new XSLFPictureData(imgPart, rel);
  108. }
  109. catch (Exception e) {
  110. throw new POIXMLException(e);
  111. }
  112. }
  113. }
  114. return _data;
  115. }
  116. /**
  117. * For an external linked picture, return the last-seen
  118. * path to the picture.
  119. * For an internal picture, returns null.
  120. */
  121. public URI getPictureLink() {
  122. if (getBlipId() != null) {
  123. // Internal picture, nothing to return
  124. return null;
  125. }
  126. String rId = getBlipLink();
  127. if (rId == null) {
  128. // No link recorded, nothing we can do
  129. return null;
  130. }
  131. PackagePart p = getSheet().getPackagePart();
  132. PackageRelationship rel = p.getRelationship(rId);
  133. if (rel != null) {
  134. return rel.getTargetURI();
  135. }
  136. return null;
  137. }
  138. private CTBlip getBlip(){
  139. CTPicture ct = (CTPicture)getXmlObject();
  140. return ct.getBlipFill().getBlip();
  141. }
  142. private String getBlipLink(){
  143. String link = getBlip().getLink();
  144. if (link.isEmpty()) return null;
  145. return link;
  146. }
  147. private String getBlipId(){
  148. String id = getBlip().getEmbed();
  149. if (id.isEmpty()) return null;
  150. return id;
  151. }
  152. @Override
  153. public Insets getClipping(){
  154. CTPicture ct = (CTPicture)getXmlObject();
  155. CTRelativeRect r = ct.getBlipFill().getSrcRect();
  156. return (r == null) ? null : new Insets(r.getT(), r.getL(), r.getB(), r.getR());
  157. }
  158. @SuppressWarnings("deprecation")
  159. @Override
  160. void copy(XSLFShape sh){
  161. super.copy(sh);
  162. XSLFPictureShape p = (XSLFPictureShape)sh;
  163. String blipId = p.getBlipId();
  164. String relId = getSheet().importBlip(blipId, p.getSheet().getPackagePart());
  165. CTPicture ct = (CTPicture)getXmlObject();
  166. CTBlip blip = ct.getBlipFill().getBlip();
  167. blip.setEmbed(relId);
  168. CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
  169. if(nvPr.isSetCustDataLst()) {
  170. // discard any custom tags associated with the picture being copied
  171. nvPr.unsetCustDataLst();
  172. }
  173. if(blip.isSetExtLst()) {
  174. CTOfficeArtExtensionList extLst = blip.getExtLst();
  175. for(CTOfficeArtExtension ext : extLst.getExtArray()){
  176. String xpath = "declare namespace a14='http://schemas.microsoft.com/office/drawing/2010/main' $this//a14:imgProps/a14:imgLayer";
  177. XmlObject[] obj = ext.selectPath(xpath);
  178. if(obj != null && obj.length == 1){
  179. XmlCursor c = obj[0].newCursor();
  180. String id = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"));//selectPath("declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' $this//[@embed]");
  181. String newId = getSheet().importBlip(id, p.getSheet().getPackagePart());
  182. c.setAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), newId);
  183. c.dispose();
  184. }
  185. }
  186. }
  187. }
  188. }