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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 java.awt.Graphics2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.io.ByteArrayInputStream;
  24. import javax.imageio.ImageIO;
  25. import javax.xml.namespace.QName;
  26. import org.apache.poi.POIXMLException;
  27. import org.apache.poi.openxml4j.opc.PackagePart;
  28. import org.apache.poi.openxml4j.opc.PackageRelationship;
  29. import org.apache.poi.util.Beta;
  30. import org.apache.xmlbeans.XmlCursor;
  31. import org.apache.xmlbeans.XmlObject;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtension;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeArtExtensionList;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.CTPictureNonVisual;
  43. /**
  44. * Represents a picture shape
  45. */
  46. @Beta
  47. public class XSLFPictureShape extends XSLFSimpleShape {
  48. private XSLFPictureData _data;
  49. /*package*/ XSLFPictureShape(CTPicture shape, XSLFSheet sheet) {
  50. super(shape, sheet);
  51. }
  52. /**
  53. * @param shapeId 1-based shapeId
  54. * @param rel relationship to the picture data in the ooxml package
  55. */
  56. static CTPicture prototype(int shapeId, String rel) {
  57. CTPicture ct = CTPicture.Factory.newInstance();
  58. CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
  59. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  60. cnv.setName("Picture " + shapeId);
  61. cnv.setId(shapeId + 1);
  62. nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
  63. nvSpPr.addNewNvPr();
  64. CTBlipFillProperties blipFill = ct.addNewBlipFill();
  65. CTBlip blip = blipFill.addNewBlip();
  66. blip.setEmbed(rel);
  67. blipFill.addNewStretch().addNewFillRect();
  68. CTShapeProperties spPr = ct.addNewSpPr();
  69. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  70. prst.setPrst(STShapeType.RECT);
  71. prst.addNewAvLst();
  72. return ct;
  73. }
  74. /**
  75. * Resize this picture to the default size.
  76. * For PNG and JPEG resizes the image to 100%,
  77. * for other types sets the default size of 200x200 pixels.
  78. */
  79. public void resize() {
  80. XSLFPictureData pict = getPictureData();
  81. try {
  82. BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  83. setAnchor(new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()));
  84. }
  85. catch (Exception e) {
  86. //default size is 200x200
  87. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  88. }
  89. }
  90. public XSLFPictureData getPictureData() {
  91. if(_data == null){
  92. String blipId = getBlipId();
  93. PackagePart p = getSheet().getPackagePart();
  94. PackageRelationship rel = p.getRelationship(blipId);
  95. if (rel != null) {
  96. try {
  97. PackagePart imgPart = p.getRelatedPart(rel);
  98. _data = new XSLFPictureData(imgPart, rel);
  99. }
  100. catch (Exception e) {
  101. throw new POIXMLException(e);
  102. }
  103. }
  104. }
  105. return _data;
  106. }
  107. private String getBlipId(){
  108. CTPicture ct = (CTPicture)getXmlObject();
  109. return ct.getBlipFill().getBlip().getEmbed();
  110. }
  111. @Override
  112. public void drawContent(Graphics2D graphics) {
  113. XSLFPictureData data = getPictureData();
  114. if(data == null) return;
  115. XSLFImageRenderer renderer = (XSLFImageRenderer)graphics.getRenderingHint(XSLFRenderingHint.IMAGE_RENDERER);
  116. if(renderer == null) renderer = new XSLFImageRenderer();
  117. RenderableShape rShape = new RenderableShape(this);
  118. Rectangle2D anchor = rShape.getAnchor(graphics);
  119. renderer.drawImage(graphics, data, anchor);
  120. }
  121. @Override
  122. void copy(XSLFShape sh){
  123. super.copy(sh);
  124. XSLFPictureShape p = (XSLFPictureShape)sh;
  125. String blipId = p.getBlipId();
  126. String relId = getSheet().importBlip(blipId, p.getSheet().getPackagePart());
  127. CTPicture ct = (CTPicture)getXmlObject();
  128. CTBlip blip = ct.getBlipFill().getBlip();
  129. blip.setEmbed(relId);
  130. CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr();
  131. if(nvPr.isSetCustDataLst()) {
  132. // discard any custom tags associated with the picture being copied
  133. nvPr.unsetCustDataLst();
  134. }
  135. if(blip.isSetExtLst()) {
  136. CTOfficeArtExtensionList extLst = blip.getExtLst();
  137. for(CTOfficeArtExtension ext : extLst.getExtList()){
  138. String xpath = "declare namespace a14='http://schemas.microsoft.com/office/drawing/2010/main' $this//a14:imgProps/a14:imgLayer";
  139. XmlObject[] obj = ext.selectPath(xpath);
  140. if(obj != null && obj.length == 1){
  141. XmlCursor c = obj[0].newCursor();
  142. String id = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"));//selectPath("declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' $this//[@embed]");
  143. String newId = getSheet().importBlip(id, p.getSheet().getPackagePart());
  144. c.setAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed"), newId);
  145. c.dispose();
  146. }
  147. }
  148. }
  149. }
  150. }