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

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