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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.Graphics2D;
  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.util.Beta;
  31. import org.apache.xmlbeans.XmlCursor;
  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.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. private XSLFPictureData _data;
  50. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  51. super(shape, sheet);
  52. }
  53. /**
  54. * @param shapeId 1-based shapeId
  55. * @param rel relationship to the picture data in the ooxml package
  56. */
  57. static CTPicture prototype(int shapeId, String rel) {
  58. CTPicture ct = CTPicture.Factory.newInstance();
  59. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  60. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  61. cnv.setName("Picture " + shapeId);
  62. cnv.setId(shapeId + 1);
  63. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  64. nvSpPr.addNewNvPr();
  65. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  66. CTBlip blip = blipFill.addNewBlip();
  67. blip.setEmbed(rel);
  68. blipFill.addNewStretch().addNewFillRect();
  69. CTShapeProperties spPr = ct.addNewSpPr();
  70. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  71. prst.setPrst(STShapeType.RECT);
  72. prst.addNewAvLst();
  73. return ct;
  74. }
  75. /**
  76. * Resize this picture to the default size.
  77. * For PNG and JPEG resizes the image to 100%,
  78. * for other types sets the default size of 200x200 pixels.
  79. */
  80. public void resize() {
  81. XSLFPictureData pict = getPictureData();
  82. try {
  83. BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  84. setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
  85. }
  86. catch (Exception e) {
  87. //default size is 200x200
  88. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  89. }
  90. }
  91. /**
  92. * Is this an internal picture (image data included within
  93. * the PowerPoint file), or an external linked picture
  94. * (image lives outside)?
  95. */
  96. public boolean isExternalLinkedPicture() {
  97. if (getBlipId() == null && getBlipLink() != null) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Return the data on the (internal) picture.
  104. * For an external linked picture, will return null
  105. */
  106. public XSLFPictureData getPictureData() {
  107. if(_data == null){
  108. String blipId = getBlipId();
  109. if (blipId == null) return null;
  110. PackagePart p = getSheet().getPackagePart();
  111. PackageRelationship rel = p.getRelationship(blipId);
  112. if (rel != null) {
  113. try {
  114. PackagePart imgPart = p.getRelatedPart(rel);
  115. _data = new XSLFPictureData(imgPart, rel);
  116. }
  117. catch (Exception e) {
  118. throw new POIXMLException(e);
  119. }
  120. }
  121. }
  122. return _data;
  123. }
  124. /**
  125. * For an external linked picture, return the last-seen
  126. * path to the picture.
  127. * For an internal picture, returns null.
  128. */
  129. public URI getPictureLink() {
  130. if (getBlipId() != null) {
  131. // Internal picture, nothing to return
  132. return null;
  133. }
  134. String rId = getBlipLink();
  135. if (rId == null) {
  136. // No link recorded, nothing we can do
  137. return null;
  138. }
  139. PackagePart p = getSheet().getPackagePart();
  140. PackageRelationship rel = p.getRelationship(rId);
  141. if (rel != null) {
  142. return rel.getTargetURI();
  143. }
  144. return null;
  145. }
  146. private CTBlip getBlip(){
  147. CTPicture ct = (CTPicture)getXmlObject();
  148. return ct.getBlipFill().getBlip();
  149. }
  150. private String getBlipLink(){
  151. String link = getBlip().getLink();
  152. if (link.isEmpty()) return null;
  153. return link;
  154. }
  155. private String getBlipId(){
  156. String id = getBlip().getEmbed();
  157. if (id.isEmpty()) return null;
  158. return id;
  159. }
  160. @Override
  161. public void drawContent(Graphics2D graphics) {
  162. XSLFPictureData data = getPictureData();
  163. if(data == null) return;
  164. XSLFImageRenderer renderer = (XSLFImageRenderer)graphics.getRenderingHint(XSLFRenderingHint.IMAGE_RENDERER);
  165. if(renderer == null) renderer = new XSLFImageRenderer();
  166. RenderableShape rShape = new RenderableShape(this);
  167. Rectangle2D anchor = rShape.getAnchor(graphics);
  168. renderer.drawImage(graphics, data, anchor);
  169. }
  170. @Override
  171. void copy(XSLFShape sh){
  172. super.copy(sh);
  173. XSLFPictureShape p = (XSLFPictureShape)sh;
  174. String blipId = p.getBlipId();
  175. String relId = getSheet().importBlip(blipId, p.getSheet().getPackagePart());
  176. CTPicture ct = (CTPicture)getXmlObject();
  177. CTBlip blip = ct.getBlipFill().getBlip();
  178. blip.setEmbed(relId);
  179. CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
  180. if(nvPr.isSetCustDataLst()) {
  181. // discard any custom tags associated with the picture being copied
  182. nvPr.unsetCustDataLst();
  183. }
  184. if(blip.isSetExtLst()) {
  185. CTOfficeArtExtensionList extLst = blip.getExtLst();
  186. for(CTOfficeArtExtension ext : extLst.getExtArray()){
  187. String xpath = "declare namespace a14='http://schemas.microsoft.com/office/drawing/2010/main' $this//a14:imgProps/a14:imgLayer";
  188. XmlObject[] obj = ext.selectPath(xpath);
  189. if(obj != null && obj.length == 1){
  190. XmlCursor c = obj[0].newCursor();
  191. 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]");
  192. String newId = getSheet().importBlip(id, p.getSheet().getPackagePart());
  193. c.setAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), newId);
  194. c.dispose();
  195. }
  196. }
  197. }
  198. }
  199. }