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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 org.apache.poi.POIXMLException;
  21. import org.apache.poi.openxml4j.opc.PackagePart;
  22. import org.apache.poi.openxml4j.opc.PackageRelationship;
  23. import org.apache.poi.util.Beta;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
  32. import javax.imageio.ImageIO;
  33. import java.awt.Graphics2D;
  34. import java.awt.geom.Rectangle2D;
  35. import java.awt.image.BufferedImage;
  36. import java.io.ByteArrayInputStream;
  37. /**
  38. * Represents a picture shape
  39. *
  40. * @author Yegor Kozlov
  41. */
  42. @Beta
  43. public class XSLFPictureShape extends XSLFSimpleShape {
  44. private XSLFPictureData _data;
  45. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  46. super(shape, sheet);
  47. }
  48. /**
  49. * @param shapeId 1-based shapeId
  50. * @param rel relationship to the picture data in the ooxml package
  51. */
  52. static CTPicture prototype(int shapeId, String rel) {
  53. CTPicture ct = CTPicture.Factory.newInstance();
  54. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  55. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  56. cnv.setName("Picture " + shapeId);
  57. cnv.setId(shapeId + 1);
  58. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  59. nvSpPr.addNewNvPr();
  60. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  61. CTBlip blip = blipFill.addNewBlip();
  62. blip.setEmbed(rel);
  63. blipFill.addNewStretch().addNewFillRect();
  64. CTShapeProperties spPr = ct.addNewSpPr();
  65. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  66. prst.setPrst(STShapeType.RECT);
  67. prst.addNewAvLst();
  68. return ct;
  69. }
  70. /**
  71. * Resize this picture to the default size.
  72. * For PNG and JPEG resizes the image to 100%,
  73. * for other types sets the default size of 200x200 pixels.
  74. */
  75. public void resize() {
  76. XSLFPictureData pict = getPictureData();
  77. try {
  78. BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  79. setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
  80. }
  81. catch (Exception e) {
  82. //default size is 200x200
  83. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  84. }
  85. }
  86. public XSLFPictureData getPictureData() {
  87. if(_data == null){
  88. CTPicture ct = (CTPicture)getXmlObject();
  89. String blipId = ct.getBlipFill().getBlip().getEmbed();
  90. PackagePart p = getSheet().getPackagePart();
  91. PackageRelationship rel = p.getRelationship(blipId);
  92. if (rel != null) {
  93. try {
  94. PackagePart imgPart = p.getRelatedPart(rel);
  95. _data = new XSLFPictureData(imgPart, rel);
  96. }
  97. catch (Exception e) {
  98. throw new POIXMLException(e);
  99. }
  100. }
  101. }
  102. return _data;
  103. }
  104. @Override
  105. public void drawContent(Graphics2D graphics) {
  106. XSLFPictureData data = getPictureData();
  107. if(data == null) return;
  108. XSLFImageRendener renderer = (XSLFImageRendener)graphics.getRenderingHint(XSLFRenderingHint.IMAGE_RENDERER);
  109. if(renderer == null) renderer = new XSLFImageRendener();
  110. renderer.drawImage(graphics, data, getAnchor());
  111. }
  112. }