Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

XSLFSlide.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. public XSLFSlideLayout getSlideLayout(){
  126. if(_layout == null){
  127. for (POIXMLDocumentPart p : getRelations()) {
  128. if (p instanceof XSLFSlideLayout){
  129. _layout = (XSLFSlideLayout)p;
  130. }
  131. }
  132. }
  133. if(_layout == null) {
  134. throw new IllegalArgumentException("SlideLayout was not found for " + this);
  135. }
  136. return _layout;
  137. }
  138. public XSLFSlideMaster getSlideMaster(){
  139. return getSlideLayout().getSlideMaster();
  140. }
  141. /**
  142. * @return the comments part or {@code null} if there weren't any comments
  143. * @since POI 4.0.0
  144. */
  145. public XSLFComments getCommentsPart() {
  146. if(_comments == null) {
  147. for (POIXMLDocumentPart p : getRelations()) {
  148. if (p instanceof XSLFComments) {
  149. _comments = (XSLFComments)p;
  150. break;
  151. }
  152. }
  153. }
  154. return _comments;
  155. }
  156. /**
  157. * @return the comment authors part or {@code null} if there weren't any comments
  158. * @since POI 4.0.0
  159. */
  160. public XSLFCommentAuthors getCommentAuthorsPart() {
  161. if(_commentAuthors == null) {
  162. for (POIXMLDocumentPart p : getRelations()) {
  163. if (p instanceof XSLFCommentAuthors) {
  164. _commentAuthors = (XSLFCommentAuthors)p;
  165. return _commentAuthors;
  166. }
  167. }
  168. }
  169. return null;
  170. }
  171. @Override
  172. public List<XSLFComment> getComments() {
  173. final List<XSLFComment> comments = new ArrayList<>();
  174. final XSLFComments xComments = getCommentsPart();
  175. final XSLFCommentAuthors xAuthors = getCommentAuthorsPart();
  176. if (xComments != null) {
  177. for (final CTComment xc : xComments.getCTCommentsList().getCmArray()) {
  178. comments.add(new XSLFComment(xc, xAuthors));
  179. }
  180. }
  181. return comments;
  182. }
  183. @Override
  184. public XSLFNotes getNotes() {
  185. if(_notes == null) {
  186. for (POIXMLDocumentPart p : getRelations()) {
  187. if (p instanceof XSLFNotes){
  188. _notes = (XSLFNotes)p;
  189. }
  190. }
  191. }
  192. if(_notes == null) {
  193. // This slide lacks notes
  194. // Not all have them, sorry...
  195. return null;
  196. }
  197. return _notes;
  198. }
  199. @Override
  200. public String getTitle(){
  201. XSLFTextShape txt = getTextShapeByType(Placeholder.TITLE);
  202. return txt == null ? null : txt.getText();
  203. }
  204. @Override
  205. public XSLFTheme getTheme(){
  206. return getSlideLayout().getSlideMaster().getTheme();
  207. }
  208. /**
  209. *
  210. * @return the information about background appearance of this slide
  211. */
  212. @Override
  213. public XSLFBackground getBackground() {
  214. CTBackground bg = _slide.getCSld().getBg();
  215. if(bg != null) {
  216. return new XSLFBackground(bg, this);
  217. } else {
  218. return getMasterSheet().getBackground();
  219. }
  220. }
  221. @Override
  222. public boolean getFollowMasterGraphics(){
  223. return _slide.getShowMasterSp();
  224. }
  225. /**
  226. *
  227. * @param value whether shapes on the master slide should be shown or not.
  228. */
  229. public void setFollowMasterGraphics(boolean value){
  230. _slide.setShowMasterSp(value);
  231. }
  232. @Override
  233. public boolean getFollowMasterObjects() {
  234. return getFollowMasterGraphics();
  235. }
  236. @Override
  237. public void setFollowMasterObjects(boolean follow) {
  238. setFollowMasterGraphics(follow);
  239. }
  240. @Override
  241. public XSLFSlide importContent(XSLFSheet src){
  242. super.importContent(src);
  243. if (!(src instanceof XSLFSlide)) {
  244. return this;
  245. }
  246. // only copy direct backgrounds - not backgrounds of master sheet
  247. CTBackground bgOther = ((XSLFSlide)src)._slide.getCSld().getBg();
  248. if (bgOther == null) {
  249. return this;
  250. }
  251. CTBackground bgThis = _slide.getCSld().getBg();
  252. // remove existing background
  253. if (bgThis != null) {
  254. if (bgThis.isSetBgPr() && bgThis.getBgPr().isSetBlipFill()) {
  255. String oldId = bgThis.getBgPr().getBlipFill().getBlip().getEmbed();
  256. removeRelation(oldId);
  257. }
  258. _slide.getCSld().unsetBg();
  259. }
  260. bgThis = (CTBackground)_slide.getCSld().addNewBg().set(bgOther);
  261. if(bgOther.isSetBgPr() && bgOther.getBgPr().isSetBlipFill()){
  262. String idOther = bgOther.getBgPr().getBlipFill().getBlip().getEmbed();
  263. String idThis = importBlip(idOther, src.getPackagePart());
  264. bgThis.getBgPr().getBlipFill().getBlip().setEmbed(idThis);
  265. }
  266. return this;
  267. }
  268. @Override
  269. public boolean getFollowMasterBackground() {
  270. return false;
  271. }
  272. @Override
  273. @NotImplemented
  274. public void setFollowMasterBackground(boolean follow) {
  275. // not implemented ... also not in the specs
  276. throw new UnsupportedOperationException();
  277. }
  278. @Override
  279. public boolean getFollowMasterColourScheme() {
  280. return false;
  281. }
  282. @Override
  283. @NotImplemented
  284. public void setFollowMasterColourScheme(boolean follow) {
  285. // not implemented ... only for OLE objects in the specs
  286. throw new UnsupportedOperationException();
  287. }
  288. @Override
  289. @NotImplemented
  290. public void setNotes(Notes<XSLFShape,XSLFTextParagraph> notes) {
  291. assert(notes instanceof XSLFNotes);
  292. // TODO Auto-generated method stub
  293. }
  294. @Override
  295. public int getSlideNumber() {
  296. int idx = getSlideShow().getSlides().indexOf(this);
  297. return (idx == -1) ? idx : idx+1;
  298. }
  299. /**
  300. * Render this sheet into the supplied graphics object
  301. *
  302. * @param graphics
  303. */
  304. @Override
  305. public void draw(Graphics2D graphics){
  306. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  307. Drawable draw = drawFact.getDrawable(this);
  308. draw.draw(graphics);
  309. }
  310. @Override
  311. public boolean getDisplayPlaceholder(Placeholder placeholder) {
  312. return false;
  313. }
  314. @Override
  315. public void setHidden(boolean hidden) {
  316. CTSlide sld = getXmlObject();
  317. if (hidden) {
  318. sld.setShow(false);
  319. } else {
  320. // if the attribute does not exist, the slide is shown
  321. if (sld.isSetShow()) {
  322. sld.unsetShow();
  323. }
  324. }
  325. }
  326. @Override
  327. public boolean isHidden() {
  328. CTSlide sld = getXmlObject();
  329. return sld.isSetShow() && !sld.getShow();
  330. }
  331. }