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 7.6KB

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