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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.POIXMLDocumentPart;
  21. import org.apache.poi.sl.usermodel.ShapeContainer;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.poi.util.POILogger;
  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.CTSRgbColor;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
  34. import javax.imageio.ImageIO;
  35. import java.awt.Color;
  36. import java.awt.Graphics2D;
  37. import java.awt.Image;
  38. import java.awt.Rectangle;
  39. import java.awt.geom.Rectangle2D;
  40. import java.awt.image.BufferedImage;
  41. import java.io.ByteArrayInputStream;
  42. /**
  43. * @author Yegor Kozlov
  44. */
  45. @Beta
  46. public class XSLFPictureShape extends XSLFSimpleShape {
  47. private XSLFPictureData _data;
  48. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  49. super(shape, sheet);
  50. }
  51. /**
  52. * @param shapeId 1-based shapeId
  53. * @param rel relationship to the picture data in the ooxml package
  54. */
  55. static CTPicture prototype(int shapeId, String rel) {
  56. CTPicture ct = CTPicture.Factory.newInstance();
  57. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  58. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  59. cnv.setName("Picture " + shapeId);
  60. cnv.setId(shapeId + 1);
  61. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  62. nvSpPr.addNewNvPr();
  63. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  64. CTBlip blip = blipFill.addNewBlip();
  65. blip.setEmbed(rel);
  66. blipFill.addNewStretch().addNewFillRect();
  67. CTShapeProperties spPr = ct.addNewSpPr();
  68. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  69. prst.setPrst(STShapeType.RECT);
  70. prst.addNewAvLst();
  71. return ct;
  72. }
  73. /**
  74. * Resize this picture to the default size.
  75. * For PNG and JPEG resizes the image to 100%,
  76. * for other types sets the default size of 200x200 pixels.
  77. */
  78. public void resize() {
  79. XSLFPictureData pict = getPictureData();
  80. try {
  81. BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  82. setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
  83. }
  84. catch (Exception e) {
  85. //default size is 200x200
  86. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  87. }
  88. }
  89. public XSLFPictureData getPictureData() {
  90. if(_data == null){
  91. CTPicture ct = (CTPicture)getXmlObject();
  92. String blipId = ct.getBlipFill().getBlip().getEmbed();
  93. for (POIXMLDocumentPart part : getSheet().getRelations()) {
  94. if(part.getPackageRelationship().getId().equals(blipId)){
  95. _data = (XSLFPictureData)part;
  96. }
  97. }
  98. }
  99. return _data;
  100. }
  101. @Override
  102. public void draw(Graphics2D graphics){
  103. java.awt.Shape outline = getOutline();
  104. //fill
  105. Color fillColor = getFillColor();
  106. if (fillColor != null) {
  107. graphics.setColor(fillColor);
  108. applyFill(graphics);
  109. graphics.fill(outline);
  110. }
  111. // text
  112. XSLFPictureData data = getPictureData();
  113. if(data == null) return;
  114. BufferedImage img;
  115. try {
  116. img = ImageIO.read(new ByteArrayInputStream(data.getData()));
  117. }
  118. catch (Exception e){
  119. return;
  120. }
  121. Rectangle2D anchor = getAnchor();
  122. graphics.drawImage(img, (int)anchor.getX(), (int)anchor.getY(),
  123. (int)anchor.getWidth(), (int)anchor.getHeight(), null);
  124. //border overlays the image
  125. Color lineColor = getLineColor();
  126. if (lineColor != null){
  127. graphics.setColor(lineColor);
  128. applyStroke(graphics);
  129. graphics.draw(outline);
  130. }
  131. }
  132. }