Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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