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.

XSLFSheet.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.awt.Dimension;
  18. import java.awt.Graphics2D;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import javax.xml.namespace.QName;
  30. import com.zaxxer.sparsebits.SparseBitSet;
  31. import org.apache.poi.ooxml.POIXMLDocumentPart;
  32. import org.apache.poi.ooxml.POIXMLException;
  33. import org.apache.poi.openxml4j.opc.OPCPackage;
  34. import org.apache.poi.openxml4j.opc.PackageNamespaces;
  35. import org.apache.poi.openxml4j.opc.PackagePart;
  36. import org.apache.poi.openxml4j.opc.PackagePartName;
  37. import org.apache.poi.openxml4j.opc.PackageRelationship;
  38. import org.apache.poi.openxml4j.opc.TargetMode;
  39. import org.apache.poi.sl.draw.DrawFactory;
  40. import org.apache.poi.sl.draw.DrawPictureShape;
  41. import org.apache.poi.sl.draw.Drawable;
  42. import org.apache.poi.sl.usermodel.PictureData;
  43. import org.apache.poi.sl.usermodel.Placeholder;
  44. import org.apache.poi.sl.usermodel.Sheet;
  45. import org.apache.poi.util.Beta;
  46. import org.apache.poi.util.IOUtils;
  47. import org.apache.poi.util.POILogFactory;
  48. import org.apache.poi.util.POILogger;
  49. import org.apache.poi.util.Units;
  50. import org.apache.poi.xddf.usermodel.chart.XDDFChart;
  51. import org.apache.xmlbeans.XmlCursor;
  52. import org.apache.xmlbeans.XmlException;
  53. import org.apache.xmlbeans.XmlObject;
  54. import org.apache.xmlbeans.XmlOptions;
  55. import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
  56. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorMapping;
  57. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  58. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  59. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  60. import org.openxmlformats.schemas.presentationml.x2006.main.CTOleObject;
  61. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  62. import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
  63. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  64. @Beta
  65. public abstract class XSLFSheet extends POIXMLDocumentPart
  66. implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
  67. private static POILogger LOG = POILogFactory.getLogger(XSLFSheet.class);
  68. private XSLFDrawing _drawing;
  69. private List<XSLFShape> _shapes;
  70. private CTGroupShape _spTree;
  71. private XSLFTheme _theme;
  72. private List<XSLFTextShape>_placeholders;
  73. private Map<Integer, XSLFSimpleShape> _placeholderByIdMap;
  74. private Map<Integer, XSLFSimpleShape> _placeholderByTypeMap;
  75. private final SparseBitSet shapeIds = new SparseBitSet();
  76. protected XSLFSheet() {
  77. super();
  78. }
  79. /**
  80. * @since POI 3.14-Beta1
  81. */
  82. protected XSLFSheet(PackagePart part) {
  83. super(part);
  84. }
  85. /**
  86. * @return the XMLSlideShow this sheet belongs to
  87. */
  88. @Override
  89. public XMLSlideShow getSlideShow() {
  90. POIXMLDocumentPart p = getParent();
  91. while(p != null) {
  92. if(p instanceof XMLSlideShow){
  93. return (XMLSlideShow)p;
  94. }
  95. p = p.getParent();
  96. }
  97. throw new IllegalStateException("SlideShow was not found");
  98. }
  99. @SuppressWarnings("WeakerAccess")
  100. protected int allocateShapeId() {
  101. final int nextId = shapeIds.nextClearBit(1);
  102. shapeIds.set(nextId);
  103. return nextId;
  104. }
  105. @SuppressWarnings("WeakerAccess")
  106. protected void registerShapeId(final int shapeId) {
  107. if (shapeIds.get(shapeId)) {
  108. LOG.log(POILogger.WARN, "shape id "+shapeId+" has been already used.");
  109. }
  110. shapeIds.set(shapeId);
  111. }
  112. @SuppressWarnings("WeakerAccess")
  113. protected void deregisterShapeId(final int shapeId) {
  114. if (!shapeIds.get(shapeId)) {
  115. LOG.log(POILogger.WARN, "shape id "+shapeId+" hasn't been registered.");
  116. }
  117. shapeIds.clear(shapeId);
  118. }
  119. @SuppressWarnings("WeakerAccess")
  120. protected static List<XSLFShape> buildShapes(CTGroupShape spTree, XSLFShapeContainer parent){
  121. final XSLFSheet sheet = (parent instanceof XSLFSheet) ? (XSLFSheet)parent : ((XSLFShape)parent).getSheet();
  122. List<XSLFShape> shapes = new ArrayList<>();
  123. XmlCursor cur = spTree.newCursor();
  124. try {
  125. for (boolean b=cur.toFirstChild();b;b=cur.toNextSibling()) {
  126. XmlObject ch = cur.getObject();
  127. if(ch instanceof CTShape){
  128. // simple shape
  129. XSLFAutoShape shape = XSLFAutoShape.create((CTShape)ch, sheet);
  130. shapes.add(shape);
  131. } else if (ch instanceof CTGroupShape){
  132. shapes.add(new XSLFGroupShape((CTGroupShape)ch, sheet));
  133. } else if (ch instanceof CTConnector){
  134. shapes.add(new XSLFConnectorShape((CTConnector)ch, sheet));
  135. } else if (ch instanceof CTPicture){
  136. shapes.add(new XSLFPictureShape((CTPicture)ch, sheet));
  137. } else if (ch instanceof CTGraphicalObjectFrame){
  138. XSLFGraphicFrame shape = XSLFGraphicFrame.create((CTGraphicalObjectFrame)ch, sheet);
  139. shapes.add(shape);
  140. } else if (ch instanceof XmlAnyTypeImpl) {
  141. // TODO: the link of the XLSF classes to the xml beans objects will
  142. // be broken, when the elements are parsed a second time.
  143. // Unfortunately, the xml schema type can't be set of the alternate
  144. // content element
  145. cur.push();
  146. if (cur.toChild(PackageNamespaces.MARKUP_COMPATIBILITY, "Choice") && cur.toFirstChild()) {
  147. try {
  148. CTGroupShape grp = CTGroupShape.Factory.parse(cur.newXMLStreamReader());
  149. shapes.addAll(buildShapes(grp, parent));
  150. } catch (XmlException e) {
  151. LOG.log(POILogger.DEBUG, "unparsable alternate content", e);
  152. }
  153. }
  154. cur.pop();
  155. }
  156. }
  157. } finally {
  158. cur.dispose();
  159. }
  160. for (final XSLFShape s : shapes) {
  161. s.setParent(parent);
  162. }
  163. return shapes;
  164. }
  165. /**
  166. * @return top-level Xml bean representing this sheet
  167. */
  168. public abstract XmlObject getXmlObject();
  169. private XSLFDrawing getDrawing(){
  170. initDrawingAndShapes();
  171. return _drawing;
  172. }
  173. /**
  174. * Returns an array containing all of the shapes in this sheet
  175. *
  176. * @return an array of all shapes in this sheet
  177. */
  178. @Override
  179. public List<XSLFShape> getShapes(){
  180. initDrawingAndShapes();
  181. return _shapes;
  182. }
  183. /**
  184. * Helper method for initializing drawing and shapes in one go.
  185. * If they are initialized separately, there's a risk that shapes
  186. * get added twice, e.g. a shape is added to the drawing, then
  187. * buildShapes is called and at last the shape is added to shape list
  188. */
  189. private void initDrawingAndShapes() {
  190. CTGroupShape cgs = getSpTree();
  191. if(_drawing == null) {
  192. _drawing = new XSLFDrawing(this, cgs);
  193. }
  194. if (_shapes == null) {
  195. _shapes = buildShapes(cgs, this);
  196. }
  197. }
  198. // shape factory methods
  199. @Override
  200. public XSLFAutoShape createAutoShape(){
  201. XSLFAutoShape sh = getDrawing().createAutoShape();
  202. getShapes().add(sh);
  203. sh.setParent(this);
  204. return sh;
  205. }
  206. @Override
  207. public XSLFFreeformShape createFreeform(){
  208. XSLFFreeformShape sh = getDrawing().createFreeform();
  209. getShapes().add(sh);
  210. sh.setParent(this);
  211. return sh;
  212. }
  213. @Override
  214. public XSLFTextBox createTextBox(){
  215. XSLFTextBox sh = getDrawing().createTextBox();
  216. getShapes().add(sh);
  217. sh.setParent(this);
  218. return sh;
  219. }
  220. @Override
  221. public XSLFConnectorShape createConnector(){
  222. XSLFConnectorShape sh = getDrawing().createConnector();
  223. getShapes().add(sh);
  224. sh.setParent(this);
  225. return sh;
  226. }
  227. @Override
  228. public XSLFGroupShape createGroup(){
  229. XSLFGroupShape sh = getDrawing().createGroup();
  230. getShapes().add(sh);
  231. sh.setParent(this);
  232. return sh;
  233. }
  234. @Override
  235. public XSLFPictureShape createPicture(PictureData pictureData){
  236. if (!(pictureData instanceof XSLFPictureData)) {
  237. throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData");
  238. }
  239. RelationPart rp = addRelation(null, XSLFRelation.IMAGES, (XSLFPictureData)pictureData);
  240. XSLFPictureShape sh = getDrawing().createPicture(rp.getRelationship().getId());
  241. new DrawPictureShape(sh).resize();
  242. getShapes().add(sh);
  243. sh.setParent(this);
  244. return sh;
  245. }
  246. public XSLFTable createTable(){
  247. XSLFTable sh = getDrawing().createTable();
  248. getShapes().add(sh);
  249. sh.setParent(this);
  250. return sh;
  251. }
  252. @Override
  253. public XSLFTable createTable(int numRows, int numCols){
  254. if (numRows < 1 || numCols < 1) {
  255. throw new IllegalArgumentException("numRows and numCols must be greater than 0");
  256. }
  257. XSLFTable sh = getDrawing().createTable();
  258. getShapes().add(sh);
  259. sh.setParent(this);
  260. for (int r=0; r<numRows; r++) {
  261. XSLFTableRow row = sh.addRow();
  262. for (int c=0; c<numCols; c++) {
  263. row.addCell();
  264. }
  265. }
  266. return sh;
  267. }
  268. @Override
  269. public XSLFObjectShape createOleShape(PictureData pictureData) {
  270. if (!(pictureData instanceof XSLFPictureData)) {
  271. throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData");
  272. }
  273. RelationPart rp = addRelation(null, XSLFRelation.IMAGES, (XSLFPictureData)pictureData);
  274. XSLFObjectShape sh = getDrawing().createOleShape(rp.getRelationship().getId());
  275. CTOleObject oleObj = sh.getCTOleObject();
  276. Dimension dim = pictureData.getImageDimension();
  277. oleObj.setImgW(Units.toEMU(dim.getWidth()));
  278. oleObj.setImgH(Units.toEMU(dim.getHeight()));
  279. getShapes().add(sh);
  280. sh.setParent(this);
  281. return sh;
  282. }
  283. /**
  284. * Returns an iterator over the shapes in this sheet
  285. *
  286. * @return an iterator over the shapes in this sheet
  287. */
  288. @Override
  289. public Iterator<XSLFShape> iterator(){
  290. return getShapes().iterator();
  291. }
  292. @Override
  293. public void addShape(XSLFShape shape) {
  294. throw new UnsupportedOperationException(
  295. "Adding a shape from a different container is not supported -"
  296. + " create it from scratch witht XSLFSheet.create* methods");
  297. }
  298. /**
  299. * Removes the specified shape from this sheet, if it is present
  300. * (optional operation). If this sheet does not contain the element,
  301. * it is unchanged.
  302. *
  303. * @param xShape shape to be removed from this sheet, if present
  304. * @return <tt>true</tt> if this sheet contained the specified element
  305. * @throws IllegalArgumentException if the type of the specified shape
  306. * is incompatible with this sheet (optional)
  307. */
  308. @Override
  309. public boolean removeShape(XSLFShape xShape) {
  310. XmlObject obj = xShape.getXmlObject();
  311. CTGroupShape spTree = getSpTree();
  312. deregisterShapeId(xShape.getShapeId());
  313. if(obj instanceof CTShape){
  314. spTree.getSpList().remove(obj);
  315. } else if (obj instanceof CTGroupShape) {
  316. XSLFGroupShape gs = (XSLFGroupShape)xShape;
  317. new ArrayList<>(gs.getShapes()).forEach(gs::removeShape);
  318. spTree.getGrpSpList().remove(obj);
  319. } else if (obj instanceof CTConnector) {
  320. spTree.getCxnSpList().remove(obj);
  321. } else if (obj instanceof CTGraphicalObjectFrame) {
  322. spTree.getGraphicFrameList().remove(obj);
  323. } else if (obj instanceof CTPicture) {
  324. XSLFPictureShape ps = (XSLFPictureShape)xShape;
  325. removePictureRelation(ps);
  326. spTree.getPicList().remove(obj);
  327. } else {
  328. throw new IllegalArgumentException("Unsupported shape: " + xShape);
  329. }
  330. return getShapes().remove(xShape);
  331. }
  332. /**
  333. * Removes all of the elements from this container (optional operation).
  334. * The container will be empty after this call returns.
  335. */
  336. @Override
  337. public void clear() {
  338. List<XSLFShape> shapes = new ArrayList<>(getShapes());
  339. for(XSLFShape shape : shapes){
  340. removeShape(shape);
  341. }
  342. }
  343. protected abstract String getRootElementName();
  344. @SuppressWarnings("WeakerAccess")
  345. protected CTGroupShape getSpTree(){
  346. if(_spTree == null) {
  347. XmlObject root = getXmlObject();
  348. XmlObject[] sp = root.selectPath(
  349. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:spTree");
  350. if(sp.length == 0) {
  351. throw new IllegalStateException("CTGroupShape was not found");
  352. }
  353. _spTree = (CTGroupShape)sp[0];
  354. }
  355. return _spTree;
  356. }
  357. @Override
  358. protected final void commit() throws IOException {
  359. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  360. String docName = getRootElementName();
  361. if(docName != null) {
  362. xmlOptions.setSaveSyntheticDocumentElement(
  363. new QName("http://schemas.openxmlformats.org/presentationml/2006/main", docName));
  364. }
  365. PackagePart part = getPackagePart();
  366. OutputStream out = part.getOutputStream();
  367. getXmlObject().save(out, xmlOptions);
  368. out.close();
  369. }
  370. /**
  371. * Set the contents of this sheet to be a copy of the source sheet.
  372. * This method erases any existing shapes and replaces them with
  373. * object from the source sheet.
  374. *
  375. * @param src the source sheet to copy data from
  376. * @return modified 'this'
  377. */
  378. public XSLFSheet importContent(XSLFSheet src){
  379. _spTree = null;
  380. // first copy the source xml
  381. getSpTree().set(src.getSpTree().copy());
  382. wipeAndReinitialize(src, 0);
  383. return this;
  384. }
  385. private void wipeAndReinitialize(XSLFSheet src, int offset) {
  386. // explicitly initialize drawing and shapes from _spTree
  387. _shapes = null;
  388. _drawing = null;
  389. initDrawingAndShapes();
  390. // placeholders will be implicitly initialized when requested
  391. _placeholders = null;
  392. // update each shape according to its own additional copy rules
  393. List<XSLFShape> tgtShapes = getShapes();
  394. List<XSLFShape> srcShapes = src.getShapes();
  395. for(int i = 0; i < srcShapes.size(); i++){
  396. XSLFShape s1 = srcShapes.get(i);
  397. XSLFShape s2 = tgtShapes.get(offset + i);
  398. s2.copy(s1);
  399. }
  400. }
  401. /**
  402. * Append content to this sheet.
  403. *
  404. * @param src the source sheet
  405. * @return modified <code>this</code>.
  406. */
  407. @SuppressWarnings("unused")
  408. public XSLFSheet appendContent(XSLFSheet src){
  409. int numShapes = getShapes().size();
  410. CTGroupShape spTree = getSpTree();
  411. CTGroupShape srcTree = src.getSpTree();
  412. for(XmlObject ch : srcTree.selectPath("*")){
  413. if(ch instanceof CTShape){ // simple shape
  414. spTree.addNewSp().set(ch.copy());
  415. } else if (ch instanceof CTGroupShape){
  416. spTree.addNewGrpSp().set(ch.copy());
  417. } else if (ch instanceof CTConnector){
  418. spTree.addNewCxnSp().set(ch.copy());
  419. } else if (ch instanceof CTPicture){
  420. spTree.addNewPic().set(ch.copy());
  421. } else if (ch instanceof CTGraphicalObjectFrame){
  422. spTree.addNewGraphicFrame().set(ch.copy());
  423. }
  424. }
  425. wipeAndReinitialize(src, numShapes);
  426. return this;
  427. }
  428. /**
  429. * @return theme (shared styles) associated with this theme.
  430. * By default returns <code>null</code> which means that this sheet is theme-less.
  431. * Sheets that support the notion of themes (slides, masters, layouts, etc.) should override this
  432. * method and return the corresponding package part.
  433. */
  434. public XSLFTheme getTheme() {
  435. if (_theme != null || !isSupportTheme()) {
  436. return _theme;
  437. }
  438. final Optional<XSLFTheme> t =
  439. getRelations().stream().filter((p) -> p instanceof XSLFTheme).map((p) -> (XSLFTheme) p).findAny();
  440. if (t.isPresent()) {
  441. _theme = t.get();
  442. final CTColorMapping cmap = getColorMapping();
  443. if (cmap != null) {
  444. _theme.initColorMap(cmap);
  445. }
  446. }
  447. return _theme;
  448. }
  449. /**
  450. * @return {@code true} if this class supports themes
  451. */
  452. boolean isSupportTheme() {
  453. return false;
  454. }
  455. /**
  456. * @return the color mapping for this slide type
  457. */
  458. CTColorMapping getColorMapping() {
  459. return null;
  460. }
  461. @SuppressWarnings("WeakerAccess")
  462. protected XSLFTextShape getTextShapeByType(Placeholder type){
  463. for(XSLFShape shape : this.getShapes()){
  464. if(shape instanceof XSLFTextShape) {
  465. XSLFTextShape txt = (XSLFTextShape)shape;
  466. if(txt.getTextType() == type) {
  467. return txt;
  468. }
  469. }
  470. }
  471. return null;
  472. }
  473. @SuppressWarnings("WeakerAccess")
  474. public XSLFSimpleShape getPlaceholder(Placeholder ph) {
  475. return getPlaceholderByType(ph.ooxmlId);
  476. }
  477. XSLFSimpleShape getPlaceholder(CTPlaceholder ph) {
  478. XSLFSimpleShape shape = null;
  479. if(ph.isSetIdx()) {
  480. shape = getPlaceholderById((int)ph.getIdx());
  481. }
  482. if (shape == null && ph.isSetType()) {
  483. shape = getPlaceholderByType(ph.getType().intValue());
  484. }
  485. return shape;
  486. }
  487. private void initPlaceholders() {
  488. if(_placeholders == null) {
  489. _placeholders = new ArrayList<>();
  490. _placeholderByIdMap = new HashMap<>();
  491. _placeholderByTypeMap = new HashMap<>();
  492. for(final XSLFShape sh : getShapes()){
  493. if(sh instanceof XSLFTextShape){
  494. final XSLFTextShape sShape = (XSLFTextShape)sh;
  495. final CTPlaceholder ph = sShape.getPlaceholderDetails().getCTPlaceholder(false);
  496. if(ph != null) {
  497. _placeholders.add(sShape);
  498. if(ph.isSetIdx()) {
  499. int idx = (int)ph.getIdx();
  500. _placeholderByIdMap.put(idx, sShape);
  501. }
  502. if(ph.isSetType()){
  503. _placeholderByTypeMap.put(ph.getType().intValue(), sShape);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. private XSLFSimpleShape getPlaceholderById(int id) {
  511. initPlaceholders();
  512. return _placeholderByIdMap.get(id);
  513. }
  514. XSLFSimpleShape getPlaceholderByType(int ordinal) {
  515. initPlaceholders();
  516. return _placeholderByTypeMap.get(ordinal);
  517. }
  518. /**
  519. *
  520. * @param idx 0-based index of a placeholder in the sheet
  521. * @return placeholder
  522. */
  523. public XSLFTextShape getPlaceholder(int idx) {
  524. initPlaceholders();
  525. return _placeholders.get(idx);
  526. }
  527. /**
  528. *
  529. * @return all placeholder shapes in this sheet
  530. */
  531. @SuppressWarnings("WeakerAccess")
  532. public XSLFTextShape[] getPlaceholders() {
  533. initPlaceholders();
  534. return _placeholders.toArray(new XSLFTextShape[0]);
  535. }
  536. /**
  537. *
  538. * @return whether shapes on the master sheet should be shown. By default master graphics is turned off.
  539. * Sheets that support the notion of master (slide, slideLayout) should override it and
  540. * check this setting in the sheet XML
  541. */
  542. @Override
  543. public boolean getFollowMasterGraphics(){
  544. return false;
  545. }
  546. /**
  547. * @return background for this sheet
  548. */
  549. @Override
  550. public XSLFBackground getBackground() {
  551. return null;
  552. }
  553. /**
  554. * Render this sheet into the supplied graphics object
  555. *
  556. * @param graphics the graphics context to draw to
  557. */
  558. @Override
  559. public void draw(Graphics2D graphics){
  560. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  561. Drawable draw = drawFact.getDrawable(this);
  562. draw.draw(graphics);
  563. }
  564. /**
  565. * Import a picture data from another document.
  566. *
  567. * @param blipId ID of the package relationship to retrieve.
  568. * @param parent parent document containing the data to import
  569. * @return ID of the created relationship
  570. */
  571. String importBlip(String blipId, POIXMLDocumentPart parent) {
  572. final XSLFPictureData parData = parent.getRelationPartById(blipId).getDocumentPart();
  573. final XSLFPictureData pictureData;
  574. if (getPackagePart().getPackage() == parent.getPackagePart().getPackage()) {
  575. // handle ref counter correct, if the parent document is the same as this
  576. pictureData = parData;
  577. } else {
  578. XMLSlideShow ppt = getSlideShow();
  579. pictureData = ppt.addPicture(parData.getData(), parData.getType());
  580. }
  581. RelationPart rp = addRelation(blipId, XSLFRelation.IMAGES, pictureData);
  582. return rp.getRelationship().getId();
  583. }
  584. /**
  585. * Import a package part into this sheet.
  586. */
  587. void importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
  588. PackagePart destPP = getPackagePart();
  589. PackagePartName srcPPName = srcPafrt.getPartName();
  590. OPCPackage pkg = destPP.getPackage();
  591. if(pkg.containPart(srcPPName)){
  592. // already exists
  593. return;
  594. }
  595. destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
  596. PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
  597. try {
  598. OutputStream out = part.getOutputStream();
  599. InputStream is = srcPafrt.getInputStream();
  600. IOUtils.copy(is, out);
  601. is.close();
  602. out.close();
  603. } catch (IOException e){
  604. throw new POIXMLException(e);
  605. }
  606. }
  607. /**
  608. * Helper method for sheet and group shapes
  609. *
  610. * @param pictureShape the picture shapes whose relation is to be removed,
  611. * only if there are no more relations on its sheet to that picture
  612. */
  613. void removePictureRelation(XSLFPictureShape pictureShape) {
  614. int numberOfRelations = 0;
  615. String targetBlipId = pictureShape.getBlipId();
  616. for (XSLFShape shape : pictureShape.getSheet().getShapes()) {
  617. if (shape instanceof XSLFPictureShape) {
  618. XSLFPictureShape currentPictureShape = ((XSLFPictureShape) shape);
  619. String currentBlipId = currentPictureShape.getBlipId();
  620. if (currentBlipId != null && currentBlipId.equals(targetBlipId)) {
  621. numberOfRelations++;
  622. }
  623. }
  624. }
  625. if (numberOfRelations <= 1) {
  626. removeRelation(pictureShape.getBlipId());
  627. }
  628. }
  629. @Override
  630. public XSLFPlaceholderDetails getPlaceholderDetails(Placeholder placeholder) {
  631. final XSLFSimpleShape ph = getPlaceholder(placeholder);
  632. return (ph == null) ? null : new XSLFPlaceholderDetails(ph);
  633. }
  634. /**
  635. * this method will add chart into slide
  636. * with default height, width, x and y
  637. * @param chart xslf chart object
  638. * @since POI 4.1.0
  639. */
  640. public void addChart(XSLFChart chart) {
  641. Rectangle2D rect2D = new java.awt.Rectangle(XDDFChart.DEFAULT_X, XDDFChart.DEFAULT_Y,
  642. XDDFChart.DEFAULT_WIDTH, XDDFChart.DEFAULT_HEIGHT);
  643. this.addChart(chart, rect2D);
  644. }
  645. /**
  646. * this method will add chart into slide
  647. * with given height, width, x and y
  648. * @param chart xslf chart object
  649. * @since POI 4.1.0
  650. */
  651. public void addChart(XSLFChart chart, Rectangle2D rect2D) {
  652. RelationPart rp = addRelation(null, XSLFRelation.CHART, chart);
  653. getDrawing().addChart(rp.getRelationship().getId(), rect2D);
  654. }
  655. }