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.

XSLFDrawing.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.xslf.usermodel;
  16. import java.awt.Color;
  17. import java.awt.geom.Rectangle2D;
  18. import org.apache.poi.util.Beta;
  19. import org.apache.xmlbeans.XmlObject;
  20. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  21. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  22. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  23. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  24. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  25. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  26. @Beta
  27. public class XSLFDrawing {
  28. private XSLFSheet _sheet;
  29. private CTGroupShape _spTree;
  30. /*package*/ XSLFDrawing(XSLFSheet sheet, CTGroupShape spTree){
  31. _sheet = sheet;
  32. _spTree = spTree;
  33. XmlObject[] cNvPr = sheet.getSpTree().selectPath(
  34. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:cNvPr");
  35. for(XmlObject o : cNvPr) {
  36. // powerpoint generates AlternateContent elements which cNvPr elements aren't recognized
  37. // ignore them for now
  38. if (o instanceof CTNonVisualDrawingProps) {
  39. CTNonVisualDrawingProps p = (CTNonVisualDrawingProps)o;
  40. sheet.registerShapeId((int)p.getId());
  41. }
  42. }
  43. }
  44. public XSLFAutoShape createAutoShape(){
  45. CTShape sp = _spTree.addNewSp();
  46. sp.set(XSLFAutoShape.prototype(_sheet.allocateShapeId()));
  47. XSLFAutoShape shape = new XSLFAutoShape(sp, _sheet);
  48. shape.setAnchor(new Rectangle2D.Double());
  49. return shape;
  50. }
  51. public XSLFFreeformShape createFreeform(){
  52. CTShape sp = _spTree.addNewSp();
  53. sp.set(XSLFFreeformShape.prototype(_sheet.allocateShapeId()));
  54. XSLFFreeformShape shape = new XSLFFreeformShape(sp, _sheet);
  55. shape.setAnchor(new Rectangle2D.Double());
  56. return shape;
  57. }
  58. public XSLFTextBox createTextBox(){
  59. CTShape sp = _spTree.addNewSp();
  60. sp.set(XSLFTextBox.prototype(_sheet.allocateShapeId()));
  61. XSLFTextBox shape = new XSLFTextBox(sp, _sheet);
  62. shape.setAnchor(new Rectangle2D.Double());
  63. return shape;
  64. }
  65. public XSLFConnectorShape createConnector(){
  66. CTConnector sp = _spTree.addNewCxnSp();
  67. sp.set(XSLFConnectorShape.prototype(_sheet.allocateShapeId()));
  68. XSLFConnectorShape shape = new XSLFConnectorShape(sp, _sheet);
  69. shape.setAnchor(new Rectangle2D.Double());
  70. shape.setLineColor(Color.black);
  71. shape.setLineWidth(0.75);
  72. return shape;
  73. }
  74. public XSLFGroupShape createGroup(){
  75. CTGroupShape sp = _spTree.addNewGrpSp();
  76. sp.set(XSLFGroupShape.prototype(_sheet.allocateShapeId()));
  77. XSLFGroupShape shape = new XSLFGroupShape(sp, _sheet);
  78. shape.setAnchor(new Rectangle2D.Double());
  79. return shape;
  80. }
  81. public XSLFPictureShape createPicture(String rel){
  82. CTPicture sp = _spTree.addNewPic();
  83. sp.set(XSLFPictureShape.prototype(_sheet.allocateShapeId(), rel));
  84. XSLFPictureShape shape = new XSLFPictureShape(sp, _sheet);
  85. shape.setAnchor(new Rectangle2D.Double());
  86. return shape;
  87. }
  88. public XSLFTable createTable(){
  89. CTGraphicalObjectFrame sp = _spTree.addNewGraphicFrame();
  90. sp.set(XSLFTable.prototype(_sheet.allocateShapeId()));
  91. XSLFTable shape = new XSLFTable(sp, _sheet);
  92. shape.setAnchor(new Rectangle2D.Double());
  93. return shape;
  94. }
  95. /**
  96. * This method will add chart into slide's graphic frame
  97. *
  98. * @param rID relation id of chart
  99. * @param rect2D Chart Bounding values
  100. * @since POI 4.1.0
  101. */
  102. public void addChart(String rID, Rectangle2D rect2D) {
  103. CTGraphicalObjectFrame sp = _spTree.addNewGraphicFrame();
  104. sp.set(XSLFChart.prototype(_sheet.allocateShapeId(), rID, rect2D));
  105. }
  106. public XSLFObjectShape createOleShape(String pictureRel) {
  107. CTGraphicalObjectFrame sp = _spTree.addNewGraphicFrame();
  108. sp.set(XSLFObjectShape.prototype(_sheet.allocateShapeId(), pictureRel));
  109. XSLFObjectShape shape = new XSLFObjectShape(sp, _sheet);
  110. shape.setAnchor(new Rectangle2D.Double());
  111. return shape;
  112. }
  113. }