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.

XSLFTable.java 5.0KB

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.util.Internal;
  21. import org.apache.poi.util.Units;
  22. import org.apache.xmlbeans.XmlCursor;
  23. import org.apache.xmlbeans.XmlObject;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual;
  34. import javax.xml.namespace.QName;
  35. import java.util.ArrayList;
  36. import java.util.Collections;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.awt.geom.Rectangle2D;
  40. /**
  41. * Represents a table in a .pptx presentation
  42. *
  43. * @author Yegor Kozlov
  44. */
  45. public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow> {
  46. static String TABLE_URI = "http://schemas.openxmlformats.org/drawingml/2006/table";
  47. private CTTable _table;
  48. private List<XSLFTableRow> _rows;
  49. /*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  50. super(shape, sheet);
  51. for(XmlObject obj : shape.getGraphic().getGraphicData().selectPath("*")){
  52. if(obj instanceof CTTable){
  53. _table = (CTTable)obj;
  54. }
  55. }
  56. if(_table == null) throw new IllegalStateException("CTTable element was not found");
  57. _rows = new ArrayList<XSLFTableRow>(_table.sizeOfTrArray());
  58. for(CTTableRow row : _table.getTrList()) _rows.add(new XSLFTableRow(row, this));
  59. }
  60. @Internal
  61. public CTTable getCTTable(){
  62. return _table;
  63. }
  64. public int getNumberOfColumns() {
  65. return _table.getTblGrid().sizeOfGridColArray();
  66. }
  67. public int getNumberOfRows() {
  68. return _table.sizeOfTrArray();
  69. }
  70. public double getColumnWidth(int idx){
  71. return Units.toPoints(
  72. _table.getTblGrid().getGridColArray(idx).getW());
  73. }
  74. public void setColumnWidth(int idx, double width){
  75. _table.getTblGrid().getGridColArray(idx).setW(Units.toEMU(width));
  76. }
  77. public Iterator<XSLFTableRow> iterator(){
  78. return _rows.iterator();
  79. }
  80. public List<XSLFTableRow> getRows(){
  81. return Collections.unmodifiableList(_rows);
  82. }
  83. public XSLFTableRow addRow(){
  84. CTTableRow tr = _table.addNewTr();
  85. XSLFTableRow row = new XSLFTableRow(tr, this);
  86. row.setHeight(20.0); // default height is 20 points
  87. _rows.add(row);
  88. return row;
  89. }
  90. static CTGraphicalObjectFrame prototype(int shapeId){
  91. CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
  92. CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
  93. CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
  94. cnv.setName("Table " + shapeId);
  95. cnv.setId(shapeId + 1);
  96. nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
  97. nvGr.addNewNvPr();
  98. frame.addNewXfrm();
  99. CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
  100. XmlCursor cursor = gr.newCursor();
  101. cursor.toNextToken();
  102. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tbl"));
  103. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tblPr"));
  104. cursor.toNextToken();
  105. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tblGrid"));
  106. cursor.dispose();
  107. gr.setUri(TABLE_URI);
  108. return frame;
  109. }
  110. }