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ů.

XSLFSlide.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.*;
  17. import java.io.IOException;
  18. import org.apache.poi.POIXMLDocumentPart;
  19. import org.apache.poi.openxml4j.opc.PackagePart;
  20. import org.apache.poi.openxml4j.opc.PackageRelationship;
  21. import org.apache.poi.util.Beta;
  22. import org.apache.xmlbeans.XmlException;
  23. import org.apache.xmlbeans.XmlObject;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextListStyle;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
  36. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  37. @Beta
  38. public final class XSLFSlide extends XSLFSheet {
  39. private final CTSlide _slide;
  40. private XSLFSlideLayout _layout;
  41. private XSLFComments _comments;
  42. private XSLFNotes _notes;
  43. /**
  44. * Create a new slide
  45. */
  46. XSLFSlide() {
  47. super();
  48. _slide = prototype();
  49. setCommonSlideData(_slide.getCSld());
  50. }
  51. /**
  52. * Construct a SpreadsheetML slide from a package part
  53. *
  54. * @param part the package part holding the slide data,
  55. * the content type must be <code>application/vnd.openxmlformats-officedocument.slide+xml</code>
  56. * @param rel the package relationship holding this slide,
  57. * the relationship type must be http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide
  58. */
  59. XSLFSlide(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  60. super(part, rel);
  61. SldDocument doc =
  62. SldDocument.Factory.parse(getPackagePart().getInputStream());
  63. _slide = doc.getSld();
  64. setCommonSlideData(_slide.getCSld());
  65. }
  66. private static CTSlide prototype(){
  67. CTSlide ctSlide = CTSlide.Factory.newInstance();
  68. CTCommonSlideData cSld = ctSlide.addNewCSld();
  69. CTGroupShape spTree = cSld.addNewSpTree();
  70. CTGroupShapeNonVisual nvGrpSpPr = spTree.addNewNvGrpSpPr();
  71. CTNonVisualDrawingProps cnvPr = nvGrpSpPr.addNewCNvPr();
  72. cnvPr.setId(1);
  73. cnvPr.setName("");
  74. nvGrpSpPr.addNewCNvGrpSpPr();
  75. nvGrpSpPr.addNewNvPr();
  76. CTGroupShapeProperties grpSpr = spTree.addNewGrpSpPr();
  77. CTGroupTransform2D xfrm = grpSpr.addNewXfrm();
  78. CTPoint2D off = xfrm.addNewOff();
  79. off.setX(0);
  80. off.setY(0);
  81. CTPositiveSize2D ext = xfrm.addNewExt();
  82. ext.setCx(0);
  83. ext.setCy(0);
  84. CTPoint2D choff = xfrm.addNewChOff();
  85. choff.setX(0);
  86. choff.setY(0);
  87. CTPositiveSize2D chExt = xfrm.addNewChExt();
  88. chExt.setCx(0);
  89. chExt.setCy(0);
  90. ctSlide.addNewClrMapOvr().addNewMasterClrMapping();
  91. return ctSlide;
  92. }
  93. @Override
  94. public CTSlide getXmlObject() {
  95. return _slide;
  96. }
  97. @Override
  98. protected String getRootElementName(){
  99. return "sld";
  100. }
  101. public XSLFSlideMaster getMasterSheet(){
  102. return getSlideLayout().getSlideMaster();
  103. }
  104. @Override
  105. public XSLFSlideLayout getSlideLayout(){
  106. if(_layout == null){
  107. for (POIXMLDocumentPart p : getRelations()) {
  108. if (p instanceof XSLFSlideLayout){
  109. _layout = (XSLFSlideLayout)p;
  110. }
  111. }
  112. }
  113. if(_layout == null) {
  114. throw new IllegalArgumentException("SlideLayout was not found for " + this.toString());
  115. }
  116. return _layout;
  117. }
  118. @Override
  119. public XSLFSlideMaster getSlideMaster(){
  120. return getSlideLayout().getSlideMaster();
  121. }
  122. public XSLFComments getComments() {
  123. if(_comments == null) {
  124. for (POIXMLDocumentPart p : getRelations()) {
  125. if (p instanceof XSLFComments) {
  126. _comments = (XSLFComments)p;
  127. }
  128. }
  129. }
  130. if(_comments == null) {
  131. // This slide lacks comments
  132. // Not all have them, sorry...
  133. return null;
  134. }
  135. return _comments;
  136. }
  137. public XSLFNotes getNotes() {
  138. if(_notes == null) {
  139. for (POIXMLDocumentPart p : getRelations()) {
  140. if (p instanceof XSLFNotes){
  141. _notes = (XSLFNotes)p;
  142. }
  143. }
  144. }
  145. if(_notes == null) {
  146. // This slide lacks notes
  147. // Not al have them, sorry...
  148. return null;
  149. }
  150. return _notes;
  151. }
  152. public void setFollowMasterBackground(boolean value){
  153. _slide.setShowMasterSp(value);
  154. }
  155. public boolean getFollowMasterBackground(){
  156. return !_slide.isSetShowMasterSp() || _slide.getShowMasterSp();
  157. }
  158. /**
  159. *
  160. * @return title of this slide or empty string if title is not set
  161. */
  162. public String getTitle(){
  163. XSLFTextShape txt = getTextShapeByType(Placeholder.TITLE);
  164. return txt == null ? "" : txt.getText();
  165. }
  166. public XSLFTheme getTheme(){
  167. return getSlideLayout().getSlideMaster().getTheme();
  168. }
  169. @Override
  170. public void draw(Graphics2D graphics){
  171. if (getFollowMasterBackground()){
  172. XSLFSlideLayout layout = getSlideLayout();
  173. layout.draw(graphics);
  174. }
  175. super.draw(graphics);
  176. }
  177. @Override
  178. public XSLFBackground getBackground(){
  179. if(_slide.getCSld().isSetBg()) {
  180. return new XSLFBackground(_slide.getCSld().getBg(), this);
  181. }
  182. return null;
  183. }
  184. }