Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

XSLFSheet.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 org.apache.poi.POIXMLDocumentPart;
  17. import org.apache.poi.openxml4j.opc.PackagePart;
  18. import org.apache.poi.openxml4j.opc.PackageRelationship;
  19. import org.apache.poi.openxml4j.opc.TargetMode;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.xmlbeans.XmlObject;
  23. import org.apache.xmlbeans.XmlOptions;
  24. import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
  25. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  26. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  27. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  28. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  29. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  32. import javax.xml.namespace.QName;
  33. import java.awt.Graphics2D;
  34. import java.awt.geom.AffineTransform;
  35. import java.io.IOException;
  36. import java.io.OutputStream;
  37. import java.util.ArrayList;
  38. import java.util.HashMap;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.regex.Pattern;
  43. @Beta
  44. public abstract class XSLFSheet extends POIXMLDocumentPart implements Iterable<XSLFShape> {
  45. private XSLFCommonSlideData _commonSlideData;
  46. private XSLFDrawing _drawing;
  47. private List<XSLFShape> _shapes;
  48. private CTGroupShape _spTree;
  49. private List<XSLFTextShape>_placeholders;
  50. private Map<Integer, XSLFSimpleShape> _placeholderByIdMap;
  51. private Map<Integer, XSLFSimpleShape> _placeholderByTypeMap;
  52. public XSLFSheet() {
  53. super();
  54. }
  55. public XSLFSheet(PackagePart part, PackageRelationship rel){
  56. super(part, rel);
  57. }
  58. /**
  59. *
  60. * @return the XMLSlideShow this sheet belongs to
  61. */
  62. public XMLSlideShow getSlideShow() {
  63. POIXMLDocumentPart p = getParent();
  64. while(p != null) {
  65. if(p instanceof XMLSlideShow){
  66. return (XMLSlideShow)p;
  67. }
  68. p = p.getParent();
  69. }
  70. throw new IllegalStateException("SlideShow was not found");
  71. }
  72. protected List<XSLFShape> buildShapes(CTGroupShape spTree){
  73. List<XSLFShape> shapes = new ArrayList<XSLFShape>();
  74. for(XmlObject ch : spTree.selectPath("*")){
  75. if(ch instanceof CTShape){ // simple shape
  76. XSLFAutoShape shape = XSLFAutoShape.create((CTShape)ch, this);
  77. shapes.add(shape);
  78. } else if (ch instanceof CTGroupShape){
  79. shapes.add(new XSLFGroupShape((CTGroupShape)ch, this));
  80. } else if (ch instanceof CTConnector){
  81. shapes.add(new XSLFConnectorShape((CTConnector)ch, this));
  82. } else if (ch instanceof CTPicture){
  83. shapes.add(new XSLFPictureShape((CTPicture)ch, this));
  84. } else if (ch instanceof CTGraphicalObjectFrame){
  85. XSLFGraphicFrame shape = XSLFGraphicFrame.create((CTGraphicalObjectFrame)ch, this);
  86. shapes.add(shape);
  87. }
  88. }
  89. return shapes;
  90. }
  91. /**
  92. * @return top-level Xml bean representing this sheet
  93. */
  94. public abstract XmlObject getXmlObject();
  95. @Internal
  96. public XSLFCommonSlideData getCommonSlideData() {
  97. return _commonSlideData;
  98. }
  99. protected void setCommonSlideData(CTCommonSlideData data) {
  100. if(data == null) {
  101. _commonSlideData = null;
  102. } else {
  103. _commonSlideData = new XSLFCommonSlideData(data);
  104. }
  105. }
  106. private XSLFDrawing getDrawing(){
  107. if(_drawing == null) {
  108. _drawing = new XSLFDrawing(this, getSpTree());
  109. }
  110. return _drawing;
  111. }
  112. private List<XSLFShape> getShapeList(){
  113. if(_shapes == null){
  114. _shapes = buildShapes(getSpTree());
  115. }
  116. return _shapes;
  117. }
  118. // shape factory methods
  119. public XSLFAutoShape createAutoShape(){
  120. List<XSLFShape> shapes = getShapeList();
  121. XSLFAutoShape sh = getDrawing().createAutoShape();
  122. shapes.add(sh);
  123. return sh;
  124. }
  125. public XSLFFreeformShape createFreeform(){
  126. List<XSLFShape> shapes = getShapeList();
  127. XSLFFreeformShape sh = getDrawing().createFreeform();
  128. shapes.add(sh);
  129. return sh;
  130. }
  131. public XSLFTextBox createTextBox(){
  132. List<XSLFShape> shapes = getShapeList();
  133. XSLFTextBox sh = getDrawing().createTextBox();
  134. shapes.add(sh);
  135. return sh;
  136. }
  137. public XSLFConnectorShape createConnector(){
  138. List<XSLFShape> shapes = getShapeList();
  139. XSLFConnectorShape sh = getDrawing().createConnector();
  140. shapes.add(sh);
  141. return sh;
  142. }
  143. public XSLFGroupShape createGroup(){
  144. List<XSLFShape> shapes = getShapeList();
  145. XSLFGroupShape sh = getDrawing().createGroup();
  146. shapes.add(sh);
  147. return sh;
  148. }
  149. public XSLFPictureShape createPicture(int pictureIndex){
  150. List<PackagePart> pics = getPackagePart().getPackage()
  151. .getPartsByName(Pattern.compile("/ppt/media/.*?"));
  152. PackagePart pic = pics.get(pictureIndex);
  153. PackageRelationship rel = getPackagePart().addRelationship(
  154. pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
  155. addRelation(rel.getId(), new XSLFPictureData(pic, rel));
  156. XSLFPictureShape sh = getDrawing().createPicture(rel.getId());
  157. sh.resize();
  158. getShapeList().add(sh);
  159. return sh;
  160. }
  161. public XSLFTable createTable(){
  162. List<XSLFShape> shapes = getShapeList();
  163. XSLFTable sh = getDrawing().createTable();
  164. shapes.add(sh);
  165. return sh;
  166. }
  167. /**
  168. * Returns an array containing all of the shapes in this sheet
  169. *
  170. * @return an array of all shapes in this sheet
  171. */
  172. public XSLFShape[] getShapes(){
  173. return getShapeList().toArray(new XSLFShape[_shapes.size()]);
  174. }
  175. /**
  176. * Returns an iterator over the shapes in this sheet
  177. *
  178. * @return an iterator over the shapes in this sheet
  179. */
  180. public Iterator<XSLFShape> iterator(){
  181. return getShapeList().iterator();
  182. }
  183. /**
  184. * Removes the specified shape from this sheet, if it is present
  185. * (optional operation). If this sheet does not contain the element,
  186. * it is unchanged.
  187. *
  188. * @param xShape shape to be removed from this sheet, if present
  189. * @return <tt>true</tt> if this sheet contained the specified element
  190. * @throws IllegalArgumentException if the type of the specified shape
  191. * is incompatible with this sheet (optional)
  192. */
  193. public boolean removeShape(XSLFShape xShape) {
  194. XmlObject obj = xShape.getXmlObject();
  195. CTGroupShape spTree = getSpTree();
  196. if(obj instanceof CTShape){
  197. spTree.getSpList().remove(obj);
  198. } else if (obj instanceof CTGroupShape){
  199. spTree.getGrpSpList().remove(obj);
  200. } else if (obj instanceof CTConnector){
  201. spTree.getCxnSpList().remove(obj);
  202. } else {
  203. throw new IllegalArgumentException("Unsupported shape: " + xShape);
  204. }
  205. return getShapeList().remove(xShape);
  206. }
  207. protected abstract String getRootElementName();
  208. protected CTGroupShape getSpTree(){
  209. if(_spTree == null) {
  210. XmlObject root = getXmlObject();
  211. XmlObject[] sp = root.selectPath(
  212. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:spTree");
  213. if(sp.length == 0) throw new IllegalStateException("CTGroupShape was not found");
  214. _spTree = (CTGroupShape)sp[0];
  215. }
  216. return _spTree;
  217. }
  218. protected final void commit() throws IOException {
  219. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  220. Map<String, String> map = new HashMap<String, String>();
  221. map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
  222. map.put("http://schemas.openxmlformats.org/drawingml/2006/main", "a");
  223. map.put("http://schemas.openxmlformats.org/presentationml/2006/main", "p");
  224. xmlOptions.setSaveSuggestedPrefixes(map);
  225. String docName = getRootElementName();
  226. if(docName != null) {
  227. xmlOptions.setSaveSyntheticDocumentElement(
  228. new QName("http://schemas.openxmlformats.org/presentationml/2006/main", docName));
  229. }
  230. PackagePart part = getPackagePart();
  231. OutputStream out = part.getOutputStream();
  232. getXmlObject().save(out, xmlOptions);
  233. out.close();
  234. }
  235. /**
  236. * Set the contents of this sheet to be a copy of the source sheet.
  237. *
  238. * @param src the source sheet to copy data from
  239. */
  240. public void copy(XSLFSheet src){
  241. _shapes = null;
  242. _spTree = null;
  243. _drawing = null;
  244. getXmlObject().set(src.getXmlObject());
  245. }
  246. /**
  247. * @return theme (shared styles) associated with this theme.
  248. * By default returns <code>null</code> which means that this sheet is theme-less.
  249. * Sheets that support the notion of themes (slides, masters, layouts, etc.) should override this
  250. * method and return the corresposnding package part.
  251. */
  252. XSLFTheme getTheme(){
  253. return null;
  254. }
  255. /**
  256. *
  257. * @return master of this sheet.
  258. */
  259. public abstract XSLFSheet getMasterSheet();
  260. protected XSLFTextShape getTextShapeByType(Placeholder type){
  261. for(XSLFShape shape : this.getShapes()){
  262. if(shape instanceof XSLFTextShape) {
  263. XSLFTextShape txt = (XSLFTextShape)shape;
  264. if(txt.getTextType() == type) {
  265. return txt;
  266. }
  267. }
  268. }
  269. return null;
  270. }
  271. XSLFSimpleShape getPlaceholder(CTPlaceholder ph) {
  272. XSLFSimpleShape shape = null;
  273. if(ph.isSetIdx()) shape = getPlaceholderById((int)ph.getIdx());
  274. if (shape == null && ph.isSetType()) {
  275. shape = getPlaceholderByType(ph.getType().intValue());
  276. }
  277. return shape;
  278. }
  279. void initPlaceholders() {
  280. if(_placeholders == null) {
  281. _placeholders = new ArrayList<XSLFTextShape>();
  282. _placeholderByIdMap = new HashMap<Integer, XSLFSimpleShape>();
  283. _placeholderByTypeMap = new HashMap<Integer, XSLFSimpleShape>();
  284. for(XSLFShape sh : getShapes()){
  285. if(sh instanceof XSLFTextShape){
  286. XSLFTextShape sShape = (XSLFTextShape)sh;
  287. CTPlaceholder ph = sShape.getCTPlaceholder();
  288. if(ph != null) {
  289. _placeholders.add(sShape);
  290. if(ph.isSetIdx()) {
  291. int idx = (int)ph.getIdx();
  292. _placeholderByIdMap.put(idx, sShape);
  293. }
  294. if(ph.isSetType()){
  295. _placeholderByTypeMap.put(ph.getType().intValue(), sShape);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. XSLFSimpleShape getPlaceholderById(int id) {
  303. initPlaceholders();
  304. return _placeholderByIdMap.get(id);
  305. }
  306. XSLFSimpleShape getPlaceholderByType(int ordinal) {
  307. initPlaceholders();
  308. return _placeholderByTypeMap.get(ordinal);
  309. }
  310. /**
  311. *
  312. * @param idx 0-based index of a placeholder in the sheet
  313. * @return placeholder
  314. */
  315. public XSLFTextShape getPlaceholder(int idx) {
  316. initPlaceholders();
  317. return _placeholders.get(idx);
  318. }
  319. /**
  320. *
  321. * @return all placeholder shapes in this sheet
  322. */
  323. public XSLFTextShape[] getPlaceholders() {
  324. initPlaceholders();
  325. return _placeholders.toArray(new XSLFTextShape[_placeholders.size()]);
  326. }
  327. /**
  328. * Checks if this <code>sheet</code> displays the specified shape.
  329. *
  330. * Subclasses can override it and skip certain shapes from drawings,
  331. * for instance, slide masters and layouts don't display placeholders
  332. */
  333. protected boolean canDraw(XSLFShape shape){
  334. return true;
  335. }
  336. /**
  337. *
  338. * @return whether shapes on the master sheet should be shown. By default master graphics is turned off.
  339. * Sheets that support the notion of master (slide, slideLayout) should override it and
  340. * check this setting in the sheet XML
  341. */
  342. public boolean getFollowMasterGraphics(){
  343. return false;
  344. }
  345. /**
  346. * Render this sheet into the supplied graphics object
  347. *
  348. * @param graphics
  349. */
  350. public void draw(Graphics2D graphics){
  351. XSLFSheet master = getMasterSheet();
  352. if(getFollowMasterGraphics() && master != null) master.draw(graphics);
  353. for(XSLFShape shape : getShapeList()) {
  354. if(!canDraw(shape)) continue;
  355. // remember the initial transform and restore it after we are done with drawing
  356. AffineTransform at = graphics.getTransform();
  357. // concrete implementations can make sense of this hint,
  358. // for example PSGraphics2D or PDFGraphics2D would call gsave() / grestore
  359. graphics.setRenderingHint(XSLFRenderingHint.GSAVE, true);
  360. // apply rotation and flipping
  361. shape.applyTransform(graphics);
  362. // draw stuff
  363. shape.draw(graphics);
  364. // restore the coordinate system
  365. graphics.setTransform(at);
  366. graphics.setRenderingHint(XSLFRenderingHint.GRESTORE, true);
  367. }
  368. }
  369. }