Tuesday, 24 August 2021

Insert Audio and Video into PowerPoint slides using Java

We often need to create PowerPoint presentations for work, study or other reasons. Actually, in the media-rich world where we live, presentations are no longer a simple series of slides filled with text and pictures, and we can also insert video and audio into them. This tutorial will show you how it is done using Java codes with the help of a free third-party library called Free Spire.Presentation for Java.

DEPENDENCY

First of all, we need to add a Jar file called Spire.Presentation.jar to IDEA. There are two methods to do it.

Method One: Download the package of the free library from the link, find Spire.Presentation.jar in the “lib” folder and then manually add it to IDEA.

Method Two: Create a Maven project in IDEA, type the following codes in the pom.xml file and finally click the button “import changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODES

Insert Audio into PowerPoint Slide using Java Codes

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class InsertAudio {
public static void main(String[] args) throws Exception {
//create a Presentation object and load an example PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//add a shape to the first slide
Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 100, 50);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Audio:");
labelShape.getTextFrame().getTextRange().setFontHeight(28);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

//append an audio file to the slide
Rectangle2D.Double audioRect = new Rectangle2D.Double(170, 120, 50, 50);
presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:\\Users\\Test1\\Desktop\\Music.wav")).getAbsolutePath(), audioRect);

//save to file
presentation.saveToFile("output/InsertAudio.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

Output


Insert Video into PowerPoint Slide Using Java Codes

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class InsertVideo {
public static void main(String[] args) throws Exception {
//create a Presentation object and load an example PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//add a shape to the first slide
Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 100, 50);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Video:");
labelShape.getTextFrame().getTextRange().setFontHeight(28);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

//append a video file to the slide and set the cover image
Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225);
IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:\\Users\\Test1\\Desktop\\Video.mp4")).getAbsolutePath(), videoRect);
BufferedImage coverImage = ImageIO.read( new File("C:\\Users\\Test1\\Desktop\\image.png"));
video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));

//save to file
presentation.saveToFile("output/InsertVideo.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...