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.

LinkVideoToPptx.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.xslf;
  20. import static org.apache.poi.openxml4j.opc.PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS;
  21. import java.awt.Rectangle;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.net.URI;
  26. import java.net.URISyntaxException;
  27. import javax.xml.namespace.QName;
  28. import org.apache.poi.openxml4j.opc.PackagePart;
  29. import org.apache.poi.openxml4j.opc.PackageRelationship;
  30. import org.apache.poi.openxml4j.opc.TargetMode;
  31. import org.apache.poi.sl.usermodel.PictureData;
  32. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  33. import org.apache.poi.xslf.usermodel.XSLFPictureData;
  34. import org.apache.poi.xslf.usermodel.XSLFPictureShape;
  35. import org.apache.poi.xslf.usermodel.XSLFSlide;
  36. import org.apache.xmlbeans.XmlCursor;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTHyperlink;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTExtension;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.CTTLCommonMediaNodeData;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.CTTLCommonTimeNodeData;
  44. import org.openxmlformats.schemas.presentationml.x2006.main.CTTimeNodeList;
  45. import org.openxmlformats.schemas.presentationml.x2006.main.STTLTimeIndefinite;
  46. import org.openxmlformats.schemas.presentationml.x2006.main.STTLTimeNodeFillType;
  47. import org.openxmlformats.schemas.presentationml.x2006.main.STTLTimeNodeRestartType;
  48. import org.openxmlformats.schemas.presentationml.x2006.main.STTLTimeNodeType;
  49. public final class LinkVideoToPptx {
  50. private LinkVideoToPptx() {}
  51. public static void main(String[] args) throws IOException, URISyntaxException {
  52. try (XMLSlideShow pptx = new XMLSlideShow()) {
  53. String videoFileName = "file_example_MP4_640_3MG.mp4";
  54. XSLFSlide slide1 = pptx.createSlide();
  55. PackagePart pp = slide1.getPackagePart();
  56. URI mp4uri = new URI("./" + videoFileName);
  57. PackageRelationship prsEmbed1 = pp.addRelationship(mp4uri, TargetMode.EXTERNAL, "http://schemas.microsoft.com/office/2007/relationships/media");
  58. PackageRelationship prsExec1 = pp.addRelationship(mp4uri, TargetMode.EXTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/video");
  59. File previewJpg = new File("preview.jpg");
  60. XSLFPictureData snap = pptx.addPicture(previewJpg, PictureData.PictureType.JPEG);
  61. XSLFPictureShape pic1 = slide1.createPicture(snap);
  62. pic1.setAnchor(new Rectangle(100, 100, 500, 400));
  63. CTPicture xpic1 = (CTPicture) pic1.getXmlObject();
  64. CTHyperlink link1 = xpic1.getNvPicPr().getCNvPr().addNewHlinkClick();
  65. link1.setId("");
  66. link1.setAction("ppaction://media");
  67. CTApplicationNonVisualDrawingProps nvPr = xpic1.getNvPicPr().getNvPr();
  68. nvPr.addNewVideoFile().setLink(prsExec1.getId());
  69. CTExtension ext = nvPr.addNewExtLst().addNewExt();
  70. ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
  71. String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
  72. XmlCursor cur = ext.newCursor();
  73. cur.toEndToken();
  74. cur.beginElement(new QName(p14Ns, "media", "p14"));
  75. cur.insertNamespace("p14", p14Ns);
  76. cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
  77. cur.dispose();
  78. CTSlide xslide = slide1.getXmlObject();
  79. CTTimeNodeList ctnl;
  80. if (!xslide.isSetTiming()) {
  81. CTTLCommonTimeNodeData ctn = xslide.addNewTiming().addNewTnLst().addNewPar().addNewCTn();
  82. ctn.setDur(STTLTimeIndefinite.INDEFINITE);
  83. ctn.setRestart(STTLTimeNodeRestartType.NEVER);
  84. ctn.setNodeType(STTLTimeNodeType.TM_ROOT);
  85. ctnl = ctn.addNewChildTnLst();
  86. } else {
  87. ctnl = xslide.getTiming().getTnLst().getParArray(0).getCTn().getChildTnLst();
  88. }
  89. CTTLCommonMediaNodeData cmedia = ctnl.addNewVideo().addNewCMediaNode();
  90. cmedia.setVol(80000);
  91. CTTLCommonTimeNodeData ctn = cmedia.addNewCTn();
  92. ctn.setFill(STTLTimeNodeFillType.HOLD);
  93. ctn.setDisplay(false);
  94. ctn.addNewStCondLst().addNewCond().setDelay(STTLTimeIndefinite.INDEFINITE);
  95. cmedia.addNewTgtEl().addNewSpTgt().setSpid(pic1.getShapeId());
  96. try (FileOutputStream fos = new FileOutputStream("mp4test/mp4test-poi.pptx")) {
  97. pptx.write(fos);
  98. }
  99. }
  100. }
  101. }