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.

XSSFPicture.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import java.awt.Dimension;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.openxml4j.opc.PackagePart;
  22. import org.apache.poi.openxml4j.opc.PackageRelationship;
  23. import org.apache.poi.ss.usermodel.Picture;
  24. import org.apache.poi.ss.usermodel.Workbook;
  25. import org.apache.poi.ss.util.ImageUtils;
  26. import org.apache.poi.util.Internal;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualPictureProperties;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  36. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
  37. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPictureNonVisual;
  38. /**
  39. * Represents a picture shape in a SpreadsheetML drawing.
  40. */
  41. public final class XSSFPicture extends XSSFShape implements Picture {
  42. private static final Logger LOG = LogManager.getLogger(XSSFPicture.class);
  43. /*
  44. * Column width measured as the number of characters of the maximum digit width of the
  45. * numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. There are 4 pixels of margin
  46. * padding (two on each side), plus 1 pixel padding for the gridlines.
  47. *
  48. * This value is the same for default font in Office 2007 (Calibri) and Office 2003 and earlier (Arial)
  49. */
  50. // private static float DEFAULT_COLUMN_WIDTH = 9.140625f;
  51. /**
  52. * A default instance of CTShape used for creating new shapes.
  53. */
  54. private static CTPicture prototype;
  55. /**
  56. * This object specifies a picture object and all its properties
  57. */
  58. private final CTPicture ctPicture;
  59. /**
  60. * Construct a new XSSFPicture object. This constructor is called from
  61. * {@link XSSFDrawing#createPicture(XSSFClientAnchor, int)}
  62. *
  63. * @param drawing the XSSFDrawing that owns this picture
  64. */
  65. protected XSSFPicture(XSSFDrawing drawing, CTPicture ctPicture){
  66. this.drawing = drawing;
  67. this.ctPicture = ctPicture;
  68. }
  69. /**
  70. * Returns a prototype that is used to construct new shapes
  71. *
  72. * @return a prototype that is used to construct new shapes
  73. */
  74. protected static CTPicture prototype(){
  75. if(prototype == null) {
  76. CTPicture pic = CTPicture.Factory.newInstance();
  77. CTPictureNonVisual nvpr = pic.addNewNvPicPr();
  78. CTNonVisualDrawingProps nvProps = nvpr.addNewCNvPr();
  79. nvProps.setId(1);
  80. nvProps.setName("Picture 1");
  81. nvProps.setDescr("Picture");
  82. CTNonVisualPictureProperties nvPicProps = nvpr.addNewCNvPicPr();
  83. nvPicProps.addNewPicLocks().setNoChangeAspect(true);
  84. CTBlipFillProperties blip = pic.addNewBlipFill();
  85. blip.addNewBlip().setEmbed("");
  86. blip.addNewStretch().addNewFillRect();
  87. CTShapeProperties sppr = pic.addNewSpPr();
  88. CTTransform2D t2d = sppr.addNewXfrm();
  89. CTPositiveSize2D ext = t2d.addNewExt();
  90. //should be original picture width and height expressed in EMUs
  91. ext.setCx(0);
  92. ext.setCy(0);
  93. CTPoint2D off = t2d.addNewOff();
  94. off.setX(0);
  95. off.setY(0);
  96. CTPresetGeometry2D prstGeom = sppr.addNewPrstGeom();
  97. prstGeom.setPrst(STShapeType.RECT);
  98. prstGeom.addNewAvLst();
  99. prototype = pic;
  100. }
  101. return prototype;
  102. }
  103. /**
  104. * Link this shape with the picture data
  105. *
  106. * @param rel relationship referring the picture data
  107. */
  108. protected void setPictureReference(PackageRelationship rel){
  109. ctPicture.getBlipFill().getBlip().setEmbed(rel.getId());
  110. }
  111. /**
  112. * Return the underlying CTPicture bean that holds all properties for this picture
  113. *
  114. * @return the underlying CTPicture bean
  115. */
  116. @Internal
  117. public CTPicture getCTPicture(){
  118. return ctPicture;
  119. }
  120. /**
  121. * Reset the image to the dimension of the embedded image
  122. *
  123. * @see #resize(double, double)
  124. */
  125. @Override
  126. public void resize(){
  127. resize(Double.MAX_VALUE);
  128. }
  129. /**
  130. * Resize the image proportionally.
  131. *
  132. * @see #resize(double, double)
  133. */
  134. @Override
  135. public void resize(double scale) {
  136. resize(scale, scale);
  137. }
  138. /**
  139. * Resize the image relatively to its current size.
  140. * <p>
  141. * Please note, that this method works correctly only for workbooks
  142. * with the default font size (Calibri 11pt for .xlsx).
  143. * If the default font is changed the resized image can be streched vertically or horizontally.
  144. * <p>
  145. * {@code resize(1.0,1.0)} keeps the original size,<br>
  146. * {@code resize(0.5,0.5)} resize to 50% of the original,<br>
  147. * {@code resize(2.0,2.0)} resizes to 200% of the original.<br>
  148. * <code>resize({@link Double#MAX_VALUE},{@link Double#MAX_VALUE})</code> resizes to the dimension of the embedded image.
  149. *
  150. * @param scaleX the amount by which the image width is multiplied relative to the original width,
  151. * when set to {@link Double#MAX_VALUE} the width of the embedded image is used
  152. * @param scaleY the amount by which the image height is multiplied relative to the original height,
  153. * when set to {@link Double#MAX_VALUE} the height of the embedded image is used
  154. */
  155. @Override
  156. public void resize(double scaleX, double scaleY){
  157. XSSFClientAnchor anchor = getClientAnchor();
  158. XSSFClientAnchor pref = getPreferredSize(scaleX,scaleY);
  159. if (anchor == null || pref == null) {
  160. LOG.atWarn().log("picture is not anchored via client anchor - ignoring resize call");
  161. return;
  162. }
  163. int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
  164. int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
  165. anchor.setCol2(col2);
  166. // anchor.setDx1(0);
  167. anchor.setDx2(pref.getDx2());
  168. anchor.setRow2(row2);
  169. // anchor.setDy1(0);
  170. anchor.setDy2(pref.getDy2());
  171. }
  172. /**
  173. * Calculate the preferred size for this picture.
  174. *
  175. * @return XSSFClientAnchor with the preferred size for this image
  176. */
  177. @Override
  178. public XSSFClientAnchor getPreferredSize(){
  179. return getPreferredSize(1);
  180. }
  181. /**
  182. * Calculate the preferred size for this picture.
  183. *
  184. * @param scale the amount by which image dimensions are multiplied relative to the original size.
  185. * @return XSSFClientAnchor with the preferred size for this image
  186. */
  187. public XSSFClientAnchor getPreferredSize(double scale){
  188. return getPreferredSize(scale, scale);
  189. }
  190. /**
  191. * Calculate the preferred size for this picture.
  192. *
  193. * @param scaleX the amount by which image width is multiplied relative to the original width.
  194. * @param scaleY the amount by which image height is multiplied relative to the original height.
  195. * @return XSSFClientAnchor with the preferred size for this image
  196. */
  197. @Override
  198. public XSSFClientAnchor getPreferredSize(double scaleX, double scaleY){
  199. Dimension dim = ImageUtils.setPreferredSize(this, scaleX, scaleY);
  200. CTPositiveSize2D size2d = ctPicture.getSpPr().getXfrm().getExt();
  201. size2d.setCx((int)dim.getWidth());
  202. size2d.setCy((int)dim.getHeight());
  203. return getClientAnchor();
  204. }
  205. /**
  206. * Return the dimension of this image
  207. *
  208. * @param part the package part holding raw picture data
  209. * @param type type of the picture: {@link Workbook#PICTURE_TYPE_JPEG},
  210. * {@link Workbook#PICTURE_TYPE_PNG} or {@link Workbook#PICTURE_TYPE_DIB}
  211. *
  212. * @return image dimension in pixels
  213. */
  214. protected static Dimension getImageDimension(PackagePart part, int type){
  215. try (InputStream stream = part.getInputStream()) {
  216. return ImageUtils.getImageDimension(stream, type);
  217. } catch (IOException e){
  218. //return a "singulariry" if ImageIO failed to read the image
  219. LOG.atWarn().withThrowable(e).log("Failed to read image");
  220. return new Dimension();
  221. }
  222. }
  223. /**
  224. * Return the dimension of the embedded image in pixel
  225. *
  226. * @return image dimension in pixels
  227. */
  228. @Override
  229. public Dimension getImageDimension() {
  230. XSSFPictureData picData = getPictureData();
  231. return getImageDimension(picData.getPackagePart(), picData.getPictureType());
  232. }
  233. /**
  234. * Return picture data for this shape
  235. *
  236. * @return picture data for this shape
  237. */
  238. @Override
  239. public XSSFPictureData getPictureData() {
  240. String blipId = ctPicture.getBlipFill().getBlip().getEmbed();
  241. return (XSSFPictureData)getDrawing().getRelationById(blipId);
  242. }
  243. @Override
  244. protected CTShapeProperties getShapeProperties(){
  245. return ctPicture.getSpPr();
  246. }
  247. /**
  248. * @return the anchor that is used by this shape.
  249. */
  250. @Override
  251. public XSSFClientAnchor getClientAnchor() {
  252. XSSFAnchor a = getAnchor();
  253. return (a instanceof XSSFClientAnchor) ? (XSSFClientAnchor)a : null;
  254. }
  255. /**
  256. * @return the sheet which contains the picture shape
  257. */
  258. @Override
  259. public XSSFSheet getSheet() {
  260. return (XSSFSheet)getDrawing().getParent();
  261. }
  262. @Override
  263. public String getShapeName() {
  264. return ctPicture.getNvPicPr().getCNvPr().getName();
  265. }
  266. }