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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 java.util.ArrayList;
  20. import java.util.List;
  21. import org.apache.poi.POIXMLDocumentPart;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.sl.draw.DrawFactory;
  24. import org.apache.poi.sl.draw.Drawable;
  25. import org.apache.poi.sl.usermodel.Notes;
  26. import org.apache.poi.sl.usermodel.Placeholder;
  27. import org.apache.poi.sl.usermodel.Slide;
  28. import org.apache.poi.util.Beta;
  29. import org.apache.poi.util.DocumentHelper;
  30. import org.apache.poi.util.NotImplemented;
  31. import org.apache.xmlbeans.XmlException;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  37. import org.openxmlformats.schemas.presentationml.x2006.main.CTBackground;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
  44. import org.w3c.dom.Document;
  45. import org.xml.sax.SAXException;
  46. @Beta
  47. public final class XSLFSlide extends XSLFSheet
  48. implements Slide<XSLFShape,XSLFTextParagraph> {
  49. private final CTSlide _slide;
  50. private XSLFSlideLayout _layout;
  51. private XSLFComments _comments;
  52. private XSLFCommentAuthors _commentAuthors;
  53. private XSLFNotes _notes;
  54. /**
  55. * Create a new slide
  56. */
  57. XSLFSlide() {
  58. super();
  59. _slide = prototype();
  60. }
  61. /**
  62. * Construct a SpreadsheetML slide from a package part
  63. *
  64. * @param part the package part holding the slide data,
  65. * the content type must be <code>application/vnd.openxmlformats-officedocument.slide+xml</code>
  66. *
  67. * @since POI 3.14-Beta1
  68. */
  69. XSLFSlide(PackagePart part) throws IOException, XmlException {
  70. super(part);
  71. Document _doc;
  72. try {
  73. _doc = DocumentHelper.readDocument(getPackagePart().getInputStream());
  74. } catch (SAXException e) {
  75. throw new IOException(e);
  76. }
  77. SldDocument doc = SldDocument.Factory.parse(_doc, DEFAULT_XML_OPTIONS);
  78. _slide = doc.getSld();
  79. }
  80. private static CTSlide prototype(){
  81. CTSlide ctSlide = CTSlide.Factory.newInstance();
  82. CTCommonSlideData cSld = ctSlide.addNewCSld();
  83. CTGroupShape spTree = cSld.addNewSpTree();
  84. CTGroupShapeNonVisual nvGrpSpPr = spTree.addNewNvGrpSpPr();
  85. CTNonVisualDrawingProps cnvPr = nvGrpSpPr.addNewCNvPr();
  86. cnvPr.setId(1);
  87. cnvPr.setName("");
  88. nvGrpSpPr.addNewCNvGrpSpPr();
  89. nvGrpSpPr.addNewNvPr();
  90. CTGroupShapeProperties grpSpr = spTree.addNewGrpSpPr();
  91. CTGroupTransform2D xfrm = grpSpr.addNewXfrm();
  92. CTPoint2D off = xfrm.addNewOff();
  93. off.setX(0);
  94. off.setY(0);
  95. CTPositiveSize2D ext = xfrm.addNewExt();
  96. ext.setCx(0);
  97. ext.setCy(0);
  98. CTPoint2D choff = xfrm.addNewChOff();
  99. choff.setX(0);
  100. choff.setY(0);
  101. CTPositiveSize2D chExt = xfrm.addNewChExt();
  102. chExt.setCx(0);
  103. chExt.setCy(0);
  104. ctSlide.addNewClrMapOvr().addNewMasterClrMapping();
  105. return ctSlide;
  106. }
  107. @Override
  108. public CTSlide getXmlObject() {
  109. return _slide;
  110. }
  111. @Override
  112. protected String getRootElementName(){
  113. return "sld";
  114. }
  115. protected void removeChartRelation(XSLFChart chart) {
  116. removeRelation(chart);
  117. }
  118. protected void removeLayoutRelation(XSLFSlideLayout layout) {
  119. removeRelation(layout, false);
  120. }
  121. @Override
  122. public XSLFSlideLayout getMasterSheet(){
  123. return getSlideLayout();
  124. }
  125. @Override
  126. public XSLFSlideLayout getSlideLayout(){
  127. if(_layout == null){
  128. for (POIXMLDocumentPart p : getRelations()) {
  129. if (p instanceof XSLFSlideLayout){
  130. _layout = (XSLFSlideLayout)p;
  131. }
  132. }
  133. }
  134. if(_layout == null) {
  135. throw new IllegalArgumentException("SlideLayout was not found for " + this);
  136. }
  137. return _layout;
  138. }
  139. public XSLFSlideMaster getSlideMaster(){
  140. return getSlideLayout().getSlideMaster();
  141. }
  142. /**
  143. * @return the comments part or {@code null} if there weren't any comments
  144. * @since POI 4.0.0
  145. */
  146. public XSLFComments getCommentsPart() {
  147. if(_comments == null) {
  148. for (POIXMLDocumentPart p : getRelations()) {
  149. if (p instanceof XSLFComments) {
  150. _comments = (XSLFComments)p;
  151. break;
  152. }
  153. }
  154. }
  155. return _comments;
  156. }
  157. /**
  158. * @return the comment authors part or {@code null} if there weren't any comments
  159. * @since POI 4.0.0
  160. */
  161. public XSLFCommentAuthors getCommentAuthorsPart() {
  162. if(_commentAuthors == null) {
  163. // first scan the slide relations
  164. for (POIXMLDocumentPart p : getRelations()) {
  165. if (p instanceof XSLFCommentAuthors) {
  166. _commentAuthors = (XSLFCommentAuthors)p;
  167. return _commentAuthors;
  168. }
  169. }
  170. // then scan the presentation relations
  171. for (POIXMLDocumentPart p : getSlideShow().getRelations()) {
  172. if (p instanceof XSLFCommentAuthors) {
  173. _commentAuthors = (XSLFCommentAuthors)p;
  174. return _commentAuthors;
  175. }
  176. }
  177. }
  178. return null;
  179. }
  180. @Override
  181. public List<XSLFComment> getComments() {
  182. final List<XSLFComment> comments = new ArrayList<>();
  183. final XSLFComments xComments = getCommentsPart();
  184. final XSLFCommentAuthors xAuthors = getCommentAuthorsPart();
  185. if (xComments != null) {
  186. for (final CTComment xc : xComments.getCTCommentsList().getCmArray()) {
  187. comments.add(new XSLFComment(xc, xAuthors));
  188. }
  189. }
  190. return comments;
  191. }
  192. @Override
  193. public XSLFNotes getNotes() {
  194. if(_notes == null) {
  195. for (POIXMLDocumentPart p : getRelations()) {
  196. if (p instanceof XSLFNotes){
  197. _notes = (XSLFNotes)p;
  198. }
  199. }
  200. }
  201. if(_notes == null) {
  202. // This slide lacks notes
  203. // Not all have them, sorry...
  204. return null;
  205. }
  206. return _notes;
  207. }
  208. @Override
  209. public String getTitle(){
  210. XSLFTextShape txt = getTextShapeByType(Placeholder.TITLE);
  211. return txt == null ? null : txt.getText();
  212. }
  213. @Override
  214. public XSLFTheme getTheme(){
  215. return getSlideLayout().getSlideMaster().getTheme();
  216. }
  217. /**
  218. *
  219. * @return the information about background appearance of this slide
  220. */
  221. @Override
  222. public XSLFBackground getBackground() {
  223. CTBackground bg = _slide.getCSld().getBg();
  224. if(bg != null) {
  225. return new XSLFBackground(bg, this);
  226. } else {
  227. return getMasterSheet().getBackground();
  228. }
  229. }
  230. @Override
  231. public boolean getFollowMasterGraphics(){
  232. return _slide.getShowMasterSp();
  233. }
  234. /**
  235. *
  236. * @param value whether shapes on the master slide should be shown or not.
  237. */
  238. public void setFollowMasterGraphics(boolean value){
  239. _slide.setShowMasterSp(value);
  240. }
  241. @Override
  242. public boolean getFollowMasterObjects() {
  243. return getFollowMasterGraphics();
  244. }
  245. @Override
  246. public void setFollowMasterObjects(boolean follow) {
  247. setFollowMasterGraphics(follow);
  248. }
  249. @Override
  250. public XSLFSlide importContent(XSLFSheet src){
  251. super.importContent(src);
  252. if (!(src instanceof XSLFSlide)) {
  253. return this;
  254. }
  255. // only copy direct backgrounds - not backgrounds of master sheet
  256. CTBackground bgOther = ((XSLFSlide)src)._slide.getCSld().getBg();
  257. if (bgOther == null) {
  258. return this;
  259. }
  260. CTBackground bgThis = _slide.getCSld().getBg();
  261. // remove existing background
  262. if (bgThis != null) {
  263. if (bgThis.isSetBgPr() && bgThis.getBgPr().isSetBlipFill()) {
  264. String oldId = bgThis.getBgPr().getBlipFill().getBlip().getEmbed();
  265. removeRelation(oldId);
  266. }
  267. _slide.getCSld().unsetBg();
  268. }
  269. bgThis = (CTBackground)_slide.getCSld().addNewBg().set(bgOther);
  270. if(bgOther.isSetBgPr() && bgOther.getBgPr().isSetBlipFill()){
  271. String idOther = bgOther.getBgPr().getBlipFill().getBlip().getEmbed();
  272. String idThis = importBlip(idOther, src.getPackagePart());
  273. bgThis.getBgPr().getBlipFill().getBlip().setEmbed(idThis);
  274. }
  275. return this;
  276. }
  277. @Override
  278. public boolean getFollowMasterBackground() {
  279. return false;
  280. }
  281. @Override
  282. @NotImplemented
  283. public void setFollowMasterBackground(boolean follow) {
  284. // not implemented ... also not in the specs
  285. throw new UnsupportedOperationException();
  286. }
  287. @Override
  288. public boolean getFollowMasterColourScheme() {
  289. return false;
  290. }
  291. @Override
  292. @NotImplemented
  293. public void setFollowMasterColourScheme(boolean follow) {
  294. // not implemented ... only for OLE objects in the specs
  295. throw new UnsupportedOperationException();
  296. }
  297. @Override
  298. @NotImplemented
  299. public void setNotes(Notes<XSLFShape,XSLFTextParagraph> notes) {
  300. assert(notes instanceof XSLFNotes);
  301. // TODO Auto-generated method stub
  302. }
  303. @Override
  304. public int getSlideNumber() {
  305. int idx = getSlideShow().getSlides().indexOf(this);
  306. return (idx == -1) ? idx : idx+1;
  307. }
  308. /**
  309. * Render this sheet into the supplied graphics object
  310. *
  311. * @param graphics
  312. */
  313. @Override
  314. public void draw(Graphics2D graphics){
  315. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  316. Drawable draw = drawFact.getDrawable(this);
  317. draw.draw(graphics);
  318. }
  319. @Override
  320. public boolean getDisplayPlaceholder(Placeholder placeholder) {
  321. return false;
  322. }
  323. @Override
  324. public void setHidden(boolean hidden) {
  325. CTSlide sld = getXmlObject();
  326. if (hidden) {
  327. sld.setShow(false);
  328. } else {
  329. // if the attribute does not exist, the slide is shown
  330. if (sld.isSetShow()) {
  331. sld.unsetShow();
  332. }
  333. }
  334. }
  335. @Override
  336. public boolean isHidden() {
  337. CTSlide sld = getXmlObject();
  338. return sld.isSetShow() && !sld.getShow();
  339. }
  340. @Override
  341. public String getSlideName() {
  342. final CTCommonSlideData cSld = getXmlObject().getCSld();
  343. return cSld.isSetName() ? cSld.getName() : "Slide"+getSlideNumber();
  344. }
  345. }