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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.IOException;
  26. import java.io.InputStream;
  27. import java.net.URI;
  28. import javax.imageio.ImageIO;
  29. import javax.xml.namespace.QName;
  30. import javax.xml.stream.XMLStreamReader;
  31. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  32. import org.apache.logging.log4j.LogManager;
  33. import org.apache.logging.log4j.Logger;
  34. import org.apache.poi.ooxml.util.POIXMLUnits;
  35. import org.apache.poi.ooxml.util.XPathHelper;
  36. import org.apache.poi.openxml4j.opc.PackagePart;
  37. import org.apache.poi.openxml4j.opc.PackageRelationship;
  38. import org.apache.poi.sl.usermodel.PictureData;
  39. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  40. import org.apache.poi.sl.usermodel.PictureShape;
  41. import org.apache.poi.sl.usermodel.Placeholder;
  42. import org.apache.poi.util.Beta;
  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 Logger LOG = LogManager.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. @Override
  112. public XSLFPictureData getPictureData() {
  113. if(_data == null){
  114. String blipId = getBlipId();
  115. if (blipId == null) {
  116. return null;
  117. }
  118. _data = (XSLFPictureData)getSheet().getRelationById(blipId);
  119. }
  120. return _data;
  121. }
  122. @Override
  123. public void setPlaceholder(Placeholder placeholder) {
  124. super.setPlaceholder(placeholder);
  125. }
  126. /**
  127. * For an external linked picture, return the last-seen
  128. * path to the picture.
  129. * For an internal picture, returns null.
  130. */
  131. public URI getPictureLink() {
  132. if (getBlipId() != null) {
  133. // Internal picture, nothing to return
  134. return null;
  135. }
  136. String rId = getBlipLink();
  137. if (rId == null) {
  138. // No link recorded, nothing we can do
  139. return null;
  140. }
  141. PackagePart p = getSheet().getPackagePart();
  142. PackageRelationship rel = p.getRelationship(rId);
  143. if (rel != null) {
  144. return rel.getTargetURI();
  145. }
  146. return null;
  147. }
  148. protected CTBlipFillProperties getBlipFill() {
  149. CTPicture ct = (CTPicture)getXmlObject();
  150. CTBlipFillProperties bfp = ct.getBlipFill();
  151. if (bfp != null) {
  152. return bfp;
  153. }
  154. try {
  155. return XPathHelper.selectProperty(getXmlObject(), CTBlipFillProperties.class, XSLFPictureShape::parse, BLIP_FILL);
  156. } catch (XmlException xe) {
  157. return null;
  158. }
  159. }
  160. private static CTBlipFillProperties parse(XMLStreamReader reader) throws XmlException {
  161. CTPicture pic = CTPicture.Factory.parse(reader);
  162. return (pic != null) ? pic.getBlipFill() : null;
  163. }
  164. protected CTBlip getBlip(){
  165. return getBlipFill().getBlip();
  166. }
  167. @SuppressWarnings("WeakerAccess")
  168. protected String getBlipLink(){
  169. CTBlip blip = getBlip();
  170. if (blip != null) {
  171. String link = blip.getLink();
  172. return (link.isEmpty()) ? null : link;
  173. } else {
  174. return null;
  175. }
  176. }
  177. @SuppressWarnings("WeakerAccess")
  178. protected String getBlipId(){
  179. CTBlip blip = getBlip();
  180. if (blip != null) {
  181. String id = blip.getEmbed();
  182. return (id.isEmpty()) ? null : id;
  183. } else {
  184. return null;
  185. }
  186. }
  187. @Override
  188. public Insets getClipping(){
  189. CTRelativeRect r = getBlipFill().getSrcRect();
  190. return (r == null) ? null : new Insets(
  191. POIXMLUnits.parsePercent(r.xgetT()),
  192. POIXMLUnits.parsePercent(r.xgetL()),
  193. POIXMLUnits.parsePercent(r.xgetB()),
  194. POIXMLUnits.parsePercent(r.xgetR()));
  195. }
  196. /**
  197. * Add a SVG image reference
  198. * @param svgPic a previously imported svg image
  199. *
  200. * @since POI 4.1.0
  201. */
  202. public void setSvgImage(XSLFPictureData svgPic) {
  203. CTBlip blip = getBlip();
  204. CTOfficeArtExtensionList extLst = blip.isSetExtLst() ? blip.getExtLst() : blip.addNewExtLst();
  205. final int bitmapId = getExt(extLst, BITMAP_URI);
  206. CTOfficeArtExtension extBitmap;
  207. if (bitmapId == -1) {
  208. extBitmap = extLst.addNewExt();
  209. extBitmap.setUri(BITMAP_URI);
  210. XmlCursor cur = extBitmap.newCursor();
  211. cur.toEndToken();
  212. cur.beginElement(new QName(MS_DML_NS, "useLocalDpi", "a14"));
  213. cur.insertNamespace("a14", MS_DML_NS);
  214. cur.insertAttributeWithValue("val", "0");
  215. cur.dispose();
  216. }
  217. final int svgId = getExt(extLst, SVG_URI);
  218. if (svgId != -1) {
  219. extLst.removeExt(svgId);
  220. }
  221. String svgRelId = getSheet().getRelationId(svgPic);
  222. if (svgRelId == null) {
  223. svgRelId = getSheet().addRelation(null, XSLFRelation.IMAGE_SVG, svgPic).getRelationship().getId();
  224. }
  225. CTOfficeArtExtension svgBitmap = extLst.addNewExt();
  226. svgBitmap.setUri(SVG_URI);
  227. XmlCursor cur = svgBitmap.newCursor();
  228. cur.toEndToken();
  229. cur.beginElement(new QName(MS_SVG_NS, "svgBlip", "asvg"));
  230. cur.insertNamespace("asvg", MS_SVG_NS);
  231. cur.insertAttributeWithValue(EMBED_TAG, svgRelId);
  232. cur.dispose();
  233. }
  234. @Override
  235. public PictureData getAlternativePictureData() {
  236. return getSvgImage();
  237. }
  238. /**
  239. * @return picture name, can be null
  240. * @since POI 5.1.0
  241. */
  242. public String getName() {
  243. String name = null;
  244. CTPictureNonVisual nvPicPr = getCTPictureNonVisual();
  245. if (nvPicPr != null) {
  246. CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr();
  247. if (cnvdProps != null) {
  248. name = cnvdProps.getName();
  249. }
  250. }
  251. return name;
  252. }
  253. /**
  254. * @param name picture name
  255. * @return returns true if the name was set
  256. * @since POI 5.1.0
  257. */
  258. public boolean setName(String name) {
  259. XmlObject xmlObject = getXmlObject();
  260. if (xmlObject instanceof CTPicture) {
  261. CTPicture ctPicture = (CTPicture)xmlObject;
  262. CTPictureNonVisual nvPicPr = ctPicture.getNvPicPr();
  263. if (nvPicPr == null) {
  264. nvPicPr = ctPicture.addNewNvPicPr();
  265. }
  266. if (nvPicPr != null) {
  267. CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr();
  268. if (cnvdProps == null) {
  269. cnvdProps = nvPicPr.addNewCNvPr();
  270. }
  271. if (cnvdProps != null) {
  272. cnvdProps.setName(name);
  273. return true;
  274. }
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * @return SVG image data -- can return null if no SVG image is found
  281. */
  282. public XSLFPictureData getSvgImage() {
  283. CTBlip blip = getBlip();
  284. if (blip == null) {
  285. return null;
  286. }
  287. CTOfficeArtExtensionList extLst = blip.getExtLst();
  288. if (extLst == null) {
  289. return null;
  290. }
  291. int size = extLst.sizeOfExtArray();
  292. for (int i = 0; i < size; i++) {
  293. XmlCursor cur = extLst.getExtArray(i).newCursor();
  294. try {
  295. if (cur.toChild(MS_SVG_NS, "svgBlip")) {
  296. String svgRelId = cur.getAttributeText(EMBED_TAG);
  297. return (svgRelId != null) ? (XSLFPictureData) getSheet().getRelationById(svgRelId) : null;
  298. }
  299. } finally {
  300. cur.dispose();
  301. }
  302. }
  303. return null;
  304. }
  305. /**
  306. * Convenience method for adding SVG images, which generates the preview image
  307. * @param sheet the sheet to add
  308. * @param svgPic the svg picture to add
  309. * @param previewType the preview picture type or null (defaults to PNG) - currently only JPEG,GIF,PNG are allowed
  310. * @param anchor the image anchor (for calculating the preview image size) or
  311. * null (the preview size is taken from the svg picture bounds)
  312. *
  313. * @since POI 4.1.0
  314. */
  315. public static XSLFPictureShape addSvgImage(XSLFSheet sheet, XSLFPictureData svgPic, PictureType previewType, Rectangle2D anchor) throws IOException {
  316. SVGImageRenderer renderer = new SVGImageRenderer();
  317. try (InputStream is = svgPic.getInputStream()) {
  318. renderer.loadImage(is, svgPic.getType().contentType);
  319. }
  320. Dimension2D dim = renderer.getDimension();
  321. Rectangle2D anc = (anchor != null) ? anchor
  322. : new Rectangle2D.Double(0,0, Units.pixelToPoints((int)dim.getWidth()), Units.pixelToPoints((int)dim.getHeight()));
  323. PictureType pt = (previewType != null) ? previewType : PictureType.PNG;
  324. if (pt != PictureType.JPEG && pt != PictureType.GIF && pt != PictureType.PNG) {
  325. pt = PictureType.PNG;
  326. }
  327. BufferedImage thmBI = renderer.getImage(dim);
  328. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(100000);
  329. // use extension instead of enum name, because of "jpeg"
  330. ImageIO.write(thmBI, pt.extension.substring(1), bos);
  331. XSLFPictureData pngPic = sheet.getSlideShow().addPicture(bos.toInputStream(), pt);
  332. XSLFPictureShape shape = sheet.createPicture(pngPic);
  333. shape.setAnchor(anc);
  334. shape.setSvgImage(svgPic);
  335. return shape;
  336. }
  337. private int getExt(CTOfficeArtExtensionList extLst, String uri) {
  338. final int size = extLst.sizeOfExtArray();
  339. for (int i=0; i<size; i++) {
  340. CTOfficeArtExtension ext = extLst.getExtArray(i);
  341. if (uri.equals(ext.getUri())) {
  342. return i;
  343. }
  344. }
  345. return -1;
  346. }
  347. @Override
  348. void copy(XSLFShape sh) {
  349. super.copy(sh);
  350. XSLFPictureShape p = (XSLFPictureShape)sh;
  351. String blipId = p.getBlipId();
  352. if (blipId == null) {
  353. LOG.atWarn().log("unable to copy invalid picture shape");
  354. return;
  355. }
  356. String relId = getSheet().importBlip(blipId, p.getSheet());
  357. CTBlip blip = getBlipFill().getBlip();
  358. blip.setEmbed(relId);
  359. CTPictureNonVisual nvPicPr = getCTPictureNonVisual();
  360. CTApplicationNonVisualDrawingProps nvPr = nvPicPr == null ? null : nvPicPr.getNvPr();
  361. if(nvPr != null && nvPr.isSetCustDataLst()) {
  362. // discard any custom tags associated with the picture being copied
  363. nvPr.unsetCustDataLst();
  364. }
  365. if (blip.isSetExtLst()) {
  366. // TODO: check for SVG copying
  367. CTOfficeArtExtensionList extLst = blip.getExtLst();
  368. for(CTOfficeArtExtension ext : extLst.getExtArray()){
  369. String xpath = "declare namespace a14='"+ MS_DML_NS +"' $this//a14:imgProps/a14:imgLayer";
  370. XmlObject[] obj = ext.selectPath(xpath);
  371. if(obj != null && obj.length == 1){
  372. XmlCursor c = obj[0].newCursor();
  373. String id = c.getAttributeText(EMBED_TAG);
  374. String newId = getSheet().importBlip(id, p.getSheet());
  375. c.setAttributeText(EMBED_TAG, newId);
  376. c.dispose();
  377. }
  378. }
  379. }
  380. }
  381. /**
  382. * @return boolean; true if the picture is a video
  383. * @since POI 5.2.0
  384. */
  385. public boolean isVideoFile() {
  386. CTPictureNonVisual nvPicPr = getCTPictureNonVisual();
  387. if (nvPicPr != null) {
  388. CTApplicationNonVisualDrawingProps nvPr = nvPicPr.getNvPr();
  389. if (nvPr != null) {
  390. return nvPr.isSetVideoFile();
  391. }
  392. }
  393. return false;
  394. }
  395. /**
  396. * @return the link ID for the video file
  397. * @since POI 5.2.0
  398. */
  399. public String getVideoFileLink() {
  400. if (isVideoFile()) {
  401. CTPictureNonVisual nvPicPr = getCTPictureNonVisual();
  402. if (nvPicPr != null) {
  403. CTApplicationNonVisualDrawingProps nvPr = nvPicPr.getNvPr();
  404. if (nvPr != null && nvPr.getVideoFile() != null) {
  405. return nvPr.getVideoFile().getLink();
  406. }
  407. }
  408. }
  409. return null;
  410. }
  411. private CTPictureNonVisual getCTPictureNonVisual() {
  412. XmlObject xmlObject = getXmlObject();
  413. if (xmlObject instanceof CTPicture) {
  414. CTPicture ctPicture = (CTPicture) xmlObject;
  415. return ctPicture.getNvPicPr();
  416. }
  417. return null;
  418. }
  419. }