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

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