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

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