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.

XSLFSlide.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.awt.Graphics2D;
  18. import java.io.IOException;
  19. import org.apache.poi.POIXMLDocumentPart;
  20. import org.apache.poi.openxml4j.opc.PackagePart;
  21. import org.apache.poi.openxml4j.opc.PackageRelationship;
  22. import org.apache.poi.sl.draw.DrawFactory;
  23. import org.apache.poi.sl.draw.Drawable;
  24. import org.apache.poi.sl.usermodel.Notes;
  25. import org.apache.poi.sl.usermodel.Placeholder;
  26. import org.apache.poi.sl.usermodel.Slide;
  27. import org.apache.poi.util.Beta;
  28. import org.apache.poi.util.DocumentHelper;
  29. import org.apache.xmlbeans.XmlException;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  36. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  37. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
  42. import org.w3c.dom.Document;
  43. import org.xml.sax.SAXException;
  44. @Beta
  45. public final class XSLFSlide extends XSLFSheet
  46. implements Slide<XSLFShape,XSLFTextParagraph> {
  47. private final CTSlide _slide;
  48. private XSLFSlideLayout _layout;
  49. private XSLFComments _comments;
  50. private XSLFNotes _notes;
  51. /**
  52. * Create a new slide
  53. */
  54. XSLFSlide() {
  55. super();
  56. _slide = prototype();
  57. setCommonSlideData(_slide.getCSld());
  58. }
  59. /**
  60. * Construct a SpreadsheetML slide from a package part
  61. *
  62. * @param part the package part holding the slide data,
  63. * the content type must be <code>application/vnd.openxmlformats-officedocument.slide+xml</code>
  64. *
  65. * @since POI 3.14-Beta1
  66. */
  67. XSLFSlide(PackagePart part) throws IOException, XmlException {
  68. super(part);
  69. Document _doc;
  70. try {
  71. _doc = DocumentHelper.readDocument(getPackagePart().getInputStream());
  72. } catch (SAXException e) {
  73. throw new IOException(e);
  74. }
  75. SldDocument doc = SldDocument.Factory.parse(_doc, DEFAULT_XML_OPTIONS);
  76. _slide = doc.getSld();
  77. setCommonSlideData(_slide.getCSld());
  78. }
  79. /**
  80. * @deprecated in POI 3.14, scheduled for removal in POI 3.16
  81. */
  82. @Deprecated
  83. XSLFSlide(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  84. this(part);
  85. }
  86. private static CTSlide prototype(){
  87. CTSlide ctSlide = CTSlide.Factory.newInstance();
  88. CTCommonSlideData cSld = ctSlide.addNewCSld();
  89. CTGroupShape spTree = cSld.addNewSpTree();
  90. CTGroupShapeNonVisual nvGrpSpPr = spTree.addNewNvGrpSpPr();
  91. CTNonVisualDrawingProps cnvPr = nvGrpSpPr.addNewCNvPr();
  92. cnvPr.setId(1);
  93. cnvPr.setName("");
  94. nvGrpSpPr.addNewCNvGrpSpPr();
  95. nvGrpSpPr.addNewNvPr();
  96. CTGroupShapeProperties grpSpr = spTree.addNewGrpSpPr();
  97. CTGroupTransform2D xfrm = grpSpr.addNewXfrm();
  98. CTPoint2D off = xfrm.addNewOff();
  99. off.setX(0);
  100. off.setY(0);
  101. CTPositiveSize2D ext = xfrm.addNewExt();
  102. ext.setCx(0);
  103. ext.setCy(0);
  104. CTPoint2D choff = xfrm.addNewChOff();
  105. choff.setX(0);
  106. choff.setY(0);
  107. CTPositiveSize2D chExt = xfrm.addNewChExt();
  108. chExt.setCx(0);
  109. chExt.setCy(0);
  110. ctSlide.addNewClrMapOvr().addNewMasterClrMapping();
  111. return ctSlide;
  112. }
  113. @Override
  114. public CTSlide getXmlObject() {
  115. return _slide;
  116. }
  117. @Override
  118. protected String getRootElementName(){
  119. return "sld";
  120. }
  121. public XSLFSlideLayout getMasterSheet(){
  122. return getSlideLayout();
  123. }
  124. public XSLFSlideLayout getSlideLayout(){
  125. if(_layout == null){
  126. for (POIXMLDocumentPart p : getRelations()) {
  127. if (p instanceof XSLFSlideLayout){
  128. _layout = (XSLFSlideLayout)p;
  129. }
  130. }
  131. }
  132. if(_layout == null) {
  133. throw new IllegalArgumentException("SlideLayout was not found for " + this.toString());
  134. }
  135. return _layout;
  136. }
  137. public XSLFSlideMaster getSlideMaster(){
  138. return getSlideLayout().getSlideMaster();
  139. }
  140. public XSLFComments getComments() {
  141. if(_comments == null) {
  142. for (POIXMLDocumentPart p : getRelations()) {
  143. if (p instanceof XSLFComments) {
  144. _comments = (XSLFComments)p;
  145. }
  146. }
  147. }
  148. if(_comments == null) {
  149. // This slide lacks comments
  150. // Not all have them, sorry...
  151. return null;
  152. }
  153. return _comments;
  154. }
  155. public XSLFNotes getNotes() {
  156. if(_notes == null) {
  157. for (POIXMLDocumentPart p : getRelations()) {
  158. if (p instanceof XSLFNotes){
  159. _notes = (XSLFNotes)p;
  160. }
  161. }
  162. }
  163. if(_notes == null) {
  164. // This slide lacks notes
  165. // Not all have them, sorry...
  166. return null;
  167. }
  168. return _notes;
  169. }
  170. @Override
  171. public String getTitle(){
  172. XSLFTextShape txt = getTextShapeByType(Placeholder.TITLE);
  173. return txt == null ? null : txt.getText();
  174. }
  175. @Override
  176. public XSLFTheme getTheme(){
  177. return getSlideLayout().getSlideMaster().getTheme();
  178. }
  179. /**
  180. *
  181. * @return the information about background appearance of this slide
  182. */
  183. @Override
  184. public XSLFBackground getBackground() {
  185. CTBackground bg = _slide.getCSld().getBg();
  186. if(bg != null) {
  187. return new XSLFBackground(bg, this);
  188. } else {
  189. return getMasterSheet().getBackground();
  190. }
  191. }
  192. @Override
  193. public boolean getFollowMasterGraphics(){
  194. return _slide.isSetShowMasterSp() && _slide.getShowMasterSp();
  195. }
  196. /**
  197. *
  198. * @param value whether shapes on the master slide should be shown or not.
  199. */
  200. public void setFollowMasterGraphics(boolean value){
  201. _slide.setShowMasterSp(value);
  202. }
  203. public boolean getFollowMasterObjects() {
  204. return getFollowMasterGraphics();
  205. }
  206. public void setFollowMasterObjects(boolean follow) {
  207. setFollowMasterGraphics(follow);
  208. }
  209. @Override
  210. public XSLFSlide importContent(XSLFSheet src){
  211. super.importContent(src);
  212. XSLFBackground bgShape = getBackground();
  213. if(bgShape != null) {
  214. CTBackground bg = (CTBackground)bgShape.getXmlObject();
  215. if(bg.isSetBgPr() && bg.getBgPr().isSetBlipFill()){
  216. CTBlip blip = bg.getBgPr().getBlipFill().getBlip();
  217. String blipId = blip.getEmbed();
  218. String relId = importBlip(blipId, src.getPackagePart());
  219. blip.setEmbed(relId);
  220. }
  221. }
  222. return this;
  223. }
  224. public boolean getFollowMasterBackground() {
  225. return false;
  226. }
  227. public void setFollowMasterBackground(boolean follow) {
  228. // not implemented ... also not in the specs
  229. throw new UnsupportedOperationException();
  230. }
  231. public boolean getFollowMasterColourScheme() {
  232. return false;
  233. }
  234. public void setFollowMasterColourScheme(boolean follow) {
  235. // not implemented ... only for OLE objects in the specs
  236. throw new UnsupportedOperationException();
  237. }
  238. @Override
  239. public void setNotes(Notes<XSLFShape,XSLFTextParagraph> notes) {
  240. assert(notes instanceof XSLFNotes);
  241. // TODO Auto-generated method stub
  242. }
  243. @Override
  244. public int getSlideNumber() {
  245. int idx = getSlideShow().getSlides().indexOf(this);
  246. return (idx == -1) ? idx : idx+1;
  247. }
  248. /**
  249. * Render this sheet into the supplied graphics object
  250. *
  251. * @param graphics
  252. */
  253. @Override
  254. public void draw(Graphics2D graphics){
  255. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  256. Drawable draw = drawFact.getDrawable(this);
  257. draw.draw(graphics);
  258. }
  259. }