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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.net.URI;
  22. import javax.xml.namespace.QName;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.sl.usermodel.PictureShape;
  26. import org.apache.poi.sl.usermodel.Placeholder;
  27. import org.apache.poi.util.Beta;
  28. import org.apache.poi.util.POILogFactory;
  29. import org.apache.poi.util.POILogger;
  30. import org.apache.xmlbeans.XmlCursor;
  31. import org.apache.xmlbeans.XmlException;
  32. import org.apache.xmlbeans.XmlObject;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtension;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtensionList;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
  40. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  41. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  44. import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
  45. /**
  46. * Represents a picture shape
  47. */
  48. @Beta
  49. public class XSLFPictureShape extends XSLFSimpleShape
  50. implements PictureShape<XSLFShape,XSLFTextParagraph> {
  51. private static final POILogger LOG = POILogFactory.getLogger(XSLFPictureShape.class);
  52. private XSLFPictureData _data;
  53. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  54. super(shape, sheet);
  55. }
  56. /**
  57. * @param shapeId 1-based shapeId
  58. * @param rel relationship to the picture data in the ooxml package
  59. */
  60. static CTPicture prototype(int shapeId, String rel) {
  61. CTPicture ct = CTPicture.Factory.newInstance();
  62. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  63. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  64. cnv.setName("Picture " + shapeId);
  65. cnv.setId(shapeId);
  66. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  67. nvSpPr.addNewNvPr();
  68. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  69. CTBlip blip = blipFill.addNewBlip();
  70. blip.setEmbed(rel);
  71. blipFill.addNewStretch().addNewFillRect();
  72. CTShapeProperties spPr = ct.addNewSpPr();
  73. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  74. prst.setPrst(STShapeType.RECT);
  75. prst.addNewAvLst();
  76. return ct;
  77. }
  78. /**
  79. * Is this an internal picture (image data included within
  80. * the PowerPoint file), or an external linked picture
  81. * (image lives outside)?
  82. */
  83. public boolean isExternalLinkedPicture() {
  84. return getBlipId() == null && getBlipLink() != null;
  85. }
  86. /**
  87. * Return the data on the (internal) picture.
  88. * For an external linked picture, will return null
  89. */
  90. public XSLFPictureData getPictureData() {
  91. if(_data == null){
  92. String blipId = getBlipId();
  93. if (blipId == null) {
  94. return null;
  95. }
  96. _data = (XSLFPictureData)getSheet().getRelationById(blipId);
  97. }
  98. return _data;
  99. }
  100. @Override
  101. public void setPlaceholder(Placeholder placeholder) {
  102. super.setPlaceholder(placeholder);
  103. }
  104. /**
  105. * For an external linked picture, return the last-seen
  106. * path to the picture.
  107. * For an internal picture, returns null.
  108. */
  109. public URI getPictureLink() {
  110. if (getBlipId() != null) {
  111. // Internal picture, nothing to return
  112. return null;
  113. }
  114. String rId = getBlipLink();
  115. if (rId == null) {
  116. // No link recorded, nothing we can do
  117. return null;
  118. }
  119. PackagePart p = getSheet().getPackagePart();
  120. PackageRelationship rel = p.getRelationship(rId);
  121. if (rel != null) {
  122. return rel.getTargetURI();
  123. }
  124. return null;
  125. }
  126. protected CTBlipFillProperties getBlipFill() {
  127. CTPicture ct = (CTPicture)getXmlObject();
  128. CTBlipFillProperties bfp = ct.getBlipFill();
  129. if (bfp != null) {
  130. return bfp;
  131. }
  132. String xquery =
  133. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main'; "
  134. + "declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006' "
  135. + ".//mc:Fallback/p:blipFill"
  136. ;
  137. XmlObject xo = selectProperty(XmlObject.class, xquery);
  138. try {
  139. xo = CTPicture.Factory.parse(xo.getDomNode());
  140. } catch (XmlException xe) {
  141. return null;
  142. }
  143. return ((CTPicture)xo).getBlipFill();
  144. }
  145. protected CTBlip getBlip(){
  146. return getBlipFill().getBlip();
  147. }
  148. @SuppressWarnings("WeakerAccess")
  149. protected String getBlipLink(){
  150. CTBlip blip = getBlip();
  151. if (blip != null) {
  152. String link = blip.getLink();
  153. if (link.isEmpty()) return null;
  154. return link;} else {
  155. return null;
  156. }
  157. }
  158. @SuppressWarnings("WeakerAccess")
  159. protected String getBlipId(){
  160. CTBlip blip = getBlip();
  161. if (blip != null) {
  162. String id = getBlip().getEmbed();
  163. if (id.isEmpty()) return null;
  164. return id;
  165. } else {
  166. return null;
  167. }
  168. }
  169. @Override
  170. public Insets getClipping(){
  171. CTRelativeRect r = getBlipFill().getSrcRect();
  172. return (r == null) ? null : new Insets(r.getT(), r.getL(), r.getB(), r.getR());
  173. }
  174. @Override
  175. void copy(XSLFShape sh){
  176. super.copy(sh);
  177. XSLFPictureShape p = (XSLFPictureShape)sh;
  178. String blipId = p.getBlipId();
  179. if (blipId == null) {
  180. LOG.log(POILogger.WARN, "unable to copy invalid picture shape");
  181. return;
  182. }
  183. String relId = getSheet().importBlip(blipId, p.getSheet());
  184. CTPicture ct = (CTPicture)getXmlObject();
  185. CTBlip blip = getBlipFill().getBlip();
  186. blip.setEmbed(relId);
  187. CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
  188. if(nvPr.isSetCustDataLst()) {
  189. // discard any custom tags associated with the picture being copied
  190. nvPr.unsetCustDataLst();
  191. }
  192. if(blip.isSetExtLst()) {
  193. CTOfficeArtExtensionList extLst = blip.getExtLst();
  194. //noinspection deprecation
  195. for(CTOfficeArtExtension ext : extLst.getExtArray()){
  196. String xpath = "declare namespace a14='http://schemas.microsoft.com/office/drawing/2010/main' $this//a14:imgProps/a14:imgLayer";
  197. XmlObject[] obj = ext.selectPath(xpath);
  198. if(obj != null && obj.length == 1){
  199. XmlCursor c = obj[0].newCursor();
  200. 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]");
  201. String newId = getSheet().importBlip(id, p.getSheet());
  202. c.setAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), newId);
  203. c.dispose();
  204. }
  205. }
  206. }
  207. }
  208. }