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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 static org.apache.poi.openxml4j.opc.PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS;
  21. import java.awt.Insets;
  22. import java.awt.geom.Dimension2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.awt.image.BufferedImage;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.net.URI;
  30. import javax.imageio.ImageIO;
  31. import javax.xml.namespace.QName;
  32. import javax.xml.stream.XMLStreamReader;
  33. import org.apache.poi.ooxml.util.XPathHelper;
  34. import org.apache.poi.openxml4j.opc.PackagePart;
  35. import org.apache.poi.openxml4j.opc.PackageRelationship;
  36. import org.apache.poi.sl.usermodel.PictureData;
  37. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  38. import org.apache.poi.sl.usermodel.PictureShape;
  39. import org.apache.poi.sl.usermodel.Placeholder;
  40. import org.apache.poi.util.Beta;
  41. import org.apache.poi.util.POILogFactory;
  42. import org.apache.poi.util.POILogger;
  43. import org.apache.poi.util.Units;
  44. import org.apache.poi.xslf.draw.SVGImageRenderer;
  45. import org.apache.xmlbeans.XmlCursor;
  46. import org.apache.xmlbeans.XmlException;
  47. import org.apache.xmlbeans.XmlObject;
  48. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  49. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  50. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  51. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtension;
  52. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtensionList;
  53. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  54. import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
  55. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  56. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  57. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  58. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  59. import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
  60. /**
  61. * Represents a picture shape
  62. */
  63. @Beta
  64. public class XSLFPictureShape extends XSLFSimpleShape
  65. implements PictureShape<XSLFShape,XSLFTextParagraph> {
  66. private static final POILogger LOG = POILogFactory.getLogger(XSLFPictureShape.class);
  67. private static final String MS_DML_NS = "http://schemas.microsoft.com/office/drawing/2010/main";
  68. private static final String MS_SVG_NS = "http://schemas.microsoft.com/office/drawing/2016/SVG/main";
  69. private static final String BITMAP_URI = "{28A0092B-C50C-407E-A947-70E740481C1C}";
  70. private static final String SVG_URI = "{96DAC541-7B7A-43D3-8B79-37D633B846F1}";
  71. private static final QName EMBED_TAG = new QName(CORE_PROPERTIES_ECMA376_NS, "embed", "rel");
  72. private static final QName[] BLIP_FILL = { new QName(PML_NS, "blipFill") };
  73. private XSLFPictureData _data;
  74. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  75. super(shape, sheet);
  76. }
  77. /**
  78. * @param shapeId 1-based shapeId
  79. * @param rel relationship to the picture data in the ooxml package
  80. */
  81. static CTPicture prototype(int shapeId, String rel) {
  82. CTPicture ct = CTPicture.Factory.newInstance();
  83. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  84. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  85. cnv.setName("Picture " + shapeId);
  86. cnv.setId(shapeId);
  87. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  88. nvSpPr.addNewNvPr();
  89. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  90. CTBlip blip = blipFill.addNewBlip();
  91. blip.setEmbed(rel);
  92. blipFill.addNewStretch().addNewFillRect();
  93. CTShapeProperties spPr = ct.addNewSpPr();
  94. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  95. prst.setPrst(STShapeType.RECT);
  96. prst.addNewAvLst();
  97. return ct;
  98. }
  99. /**
  100. * Is this an internal picture (image data included within
  101. * the PowerPoint file), or an external linked picture
  102. * (image lives outside)?
  103. */
  104. public boolean isExternalLinkedPicture() {
  105. return getBlipId() == null && getBlipLink() != null;
  106. }
  107. /**
  108. * Return the data on the (internal) picture.
  109. * For an external linked picture, will return null
  110. */
  111. public XSLFPictureData getPictureData() {
  112. if(_data == null){
  113. String blipId = getBlipId();
  114. if (blipId == null) {
  115. return null;
  116. }
  117. _data = (XSLFPictureData)getSheet().getRelationById(blipId);
  118. }
  119. return _data;
  120. }
  121. @Override
  122. public void setPlaceholder(Placeholder placeholder) {
  123. super.setPlaceholder(placeholder);
  124. }
  125. /**
  126. * For an external linked picture, return the last-seen
  127. * path to the picture.
  128. * For an internal picture, returns null.
  129. */
  130. public URI getPictureLink() {
  131. if (getBlipId() != null) {
  132. // Internal picture, nothing to return
  133. return null;
  134. }
  135. String rId = getBlipLink();
  136. if (rId == null) {
  137. // No link recorded, nothing we can do
  138. return null;
  139. }
  140. PackagePart p = getSheet().getPackagePart();
  141. PackageRelationship rel = p.getRelationship(rId);
  142. if (rel != null) {
  143. return rel.getTargetURI();
  144. }
  145. return null;
  146. }
  147. protected CTBlipFillProperties getBlipFill() {
  148. CTPicture ct = (CTPicture)getXmlObject();
  149. CTBlipFillProperties bfp = ct.getBlipFill();
  150. if (bfp != null) {
  151. return bfp;
  152. }
  153. try {
  154. return XPathHelper.selectProperty(getXmlObject(), CTBlipFillProperties.class, XSLFPictureShape::parse, BLIP_FILL);
  155. } catch (XmlException xe) {
  156. return null;
  157. }
  158. }
  159. private static CTBlipFillProperties parse(XMLStreamReader reader) throws XmlException {
  160. CTPicture pic = CTPicture.Factory.parse(reader);
  161. return (pic != null) ? pic.getBlipFill() : null;
  162. }
  163. protected CTBlip getBlip(){
  164. return getBlipFill().getBlip();
  165. }
  166. @SuppressWarnings("WeakerAccess")
  167. protected String getBlipLink(){
  168. CTBlip blip = getBlip();
  169. if (blip != null) {
  170. String link = blip.getLink();
  171. return (link.isEmpty()) ? null : link;
  172. } else {
  173. return null;
  174. }
  175. }
  176. @SuppressWarnings("WeakerAccess")
  177. protected String getBlipId(){
  178. CTBlip blip = getBlip();
  179. if (blip != null) {
  180. String id = blip.getEmbed();
  181. return (id.isEmpty()) ? null : id;
  182. } else {
  183. return null;
  184. }
  185. }
  186. @Override
  187. public Insets getClipping(){
  188. CTRelativeRect r = getBlipFill().getSrcRect();
  189. return (r == null) ? null : new Insets(r.getT(), r.getL(), r.getB(), r.getR());
  190. }
  191. /**
  192. * Add a SVG image reference
  193. * @param svgPic a previously imported svg image
  194. *
  195. * @since POI 4.1.0
  196. */
  197. public void setSvgImage(XSLFPictureData svgPic) {
  198. CTBlip blip = getBlip();
  199. CTOfficeArtExtensionList extLst = blip.isSetExtLst() ? blip.getExtLst() : blip.addNewExtLst();
  200. final int bitmapId = getExt(extLst, BITMAP_URI);
  201. CTOfficeArtExtension extBitmap;
  202. if (bitmapId == -1) {
  203. extBitmap = extLst.addNewExt();
  204. extBitmap.setUri(BITMAP_URI);
  205. XmlCursor cur = extBitmap.newCursor();
  206. cur.toEndToken();
  207. cur.beginElement(new QName(MS_DML_NS, "useLocalDpi", "a14"));
  208. cur.insertNamespace("a14", MS_DML_NS);
  209. cur.insertAttributeWithValue("val", "0");
  210. cur.dispose();
  211. }
  212. final int svgId = getExt(extLst, SVG_URI);
  213. if (svgId != -1) {
  214. extLst.removeExt(svgId);
  215. }
  216. String svgRelId = getSheet().getRelationId(svgPic);
  217. if (svgRelId == null) {
  218. svgRelId = getSheet().addRelation(null, XSLFRelation.IMAGE_SVG, svgPic).getRelationship().getId();
  219. }
  220. CTOfficeArtExtension svgBitmap = extLst.addNewExt();
  221. svgBitmap.setUri(SVG_URI);
  222. XmlCursor cur = svgBitmap.newCursor();
  223. cur.toEndToken();
  224. cur.beginElement(new QName(MS_SVG_NS, "svgBlip", "asvg"));
  225. cur.insertNamespace("asvg", MS_SVG_NS);
  226. cur.insertAttributeWithValue(EMBED_TAG, svgRelId);
  227. cur.dispose();
  228. }
  229. @Override
  230. public PictureData getAlternativePictureData() {
  231. return getSvgImage();
  232. }
  233. public XSLFPictureData getSvgImage() {
  234. CTBlip blip = getBlip();
  235. if (blip == null) {
  236. return null;
  237. }
  238. CTOfficeArtExtensionList extLst = blip.getExtLst();
  239. if (extLst == null) {
  240. return null;
  241. }
  242. int size = extLst.sizeOfExtArray();
  243. for (int i = 0; i < size; i++) {
  244. XmlCursor cur = extLst.getExtArray(i).newCursor();
  245. try {
  246. if (cur.toChild(MS_SVG_NS, "svgBlip")) {
  247. String svgRelId = cur.getAttributeText(EMBED_TAG);
  248. return (svgRelId != null) ? (XSLFPictureData) getSheet().getRelationById(svgRelId) : null;
  249. }
  250. } finally {
  251. cur.dispose();
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * Convienence method for adding SVG images, which generates the preview image
  258. * @param sheet the sheet to add
  259. * @param svgPic the svg picture to add
  260. * @param previewType the preview picture type or null (defaults to PNG) - currently only JPEG,GIF,PNG are allowed
  261. * @param anchor the image anchor (for calculating the preview image size) or
  262. * null (the preview size is taken from the svg picture bounds)
  263. *
  264. * @since POI 4.1.0
  265. */
  266. public static XSLFPictureShape addSvgImage(XSLFSheet sheet, XSLFPictureData svgPic, PictureType previewType, Rectangle2D anchor) throws IOException {
  267. SVGImageRenderer renderer = new SVGImageRenderer();
  268. try (InputStream is = svgPic.getInputStream()) {
  269. renderer.loadImage(is, svgPic.getType().contentType);
  270. }
  271. Dimension2D dim = renderer.getDimension();
  272. Rectangle2D anc = (anchor != null) ? anchor
  273. : new Rectangle2D.Double(0,0, Units.pixelToPoints((int)dim.getWidth()), Units.pixelToPoints((int)dim.getHeight()));
  274. PictureType pt = (previewType != null) ? previewType : PictureType.PNG;
  275. if (pt != PictureType.JPEG || pt != PictureType.GIF || pt != PictureType.PNG) {
  276. pt = PictureType.PNG;
  277. }
  278. BufferedImage thmBI = renderer.getImage(dim);
  279. ByteArrayOutputStream bos = new ByteArrayOutputStream(100000);
  280. // use extension instead of enum name, because of "jpeg"
  281. ImageIO.write(thmBI, pt.extension.substring(1), bos);
  282. XSLFPictureData pngPic = sheet.getSlideShow().addPicture(new ByteArrayInputStream(bos.toByteArray()), pt);
  283. XSLFPictureShape shape = sheet.createPicture(pngPic);
  284. shape.setAnchor(anc);
  285. shape.setSvgImage(svgPic);
  286. return shape;
  287. }
  288. private int getExt(CTOfficeArtExtensionList extLst, String uri) {
  289. final int size = extLst.sizeOfExtArray();
  290. for (int i=0; i<size; i++) {
  291. CTOfficeArtExtension ext = extLst.getExtArray(i);
  292. if (uri.equals(ext.getUri())) {
  293. return i;
  294. }
  295. }
  296. return -1;
  297. }
  298. @Override
  299. void copy(XSLFShape sh){
  300. super.copy(sh);
  301. XSLFPictureShape p = (XSLFPictureShape)sh;
  302. String blipId = p.getBlipId();
  303. if (blipId == null) {
  304. LOG.log(POILogger.WARN, "unable to copy invalid picture shape");
  305. return;
  306. }
  307. String relId = getSheet().importBlip(blipId, p.getSheet());
  308. CTPicture ct = (CTPicture)getXmlObject();
  309. CTBlip blip = getBlipFill().getBlip();
  310. blip.setEmbed(relId);
  311. CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
  312. if(nvPr.isSetCustDataLst()) {
  313. // discard any custom tags associated with the picture being copied
  314. nvPr.unsetCustDataLst();
  315. }
  316. if(blip.isSetExtLst()) {
  317. // TODO: check for SVG copying
  318. CTOfficeArtExtensionList extLst = blip.getExtLst();
  319. //noinspection deprecation
  320. for(CTOfficeArtExtension ext : extLst.getExtArray()){
  321. String xpath = "declare namespace a14='"+ MS_DML_NS +"' $this//a14:imgProps/a14:imgLayer";
  322. XmlObject[] obj = ext.selectPath(xpath);
  323. if(obj != null && obj.length == 1){
  324. XmlCursor c = obj[0].newCursor();
  325. String id = c.getAttributeText(EMBED_TAG);
  326. String newId = getSheet().importBlip(id, p.getSheet());
  327. c.setAttributeText(EMBED_TAG, newId);
  328. c.dispose();
  329. }
  330. }
  331. }
  332. }
  333. }