Когда слайды используются для рекламной кампании или демонстрации продукта, люди обычно добавляют медиафайлы (например, аудио и видео), чтобы сделать презентацию более яркой и динамичной. В этой статье я покажу вам, как вставлять аудио- и видеофайлы в презентацию PowerPoint с помощью бесплатного Spire. Презентация для Java.
Добавьте Шпиль.Презентация.банка как зависимость
Способ 1: Скачать Бесплатный Шпиль. Презентация для Java пакет, распакуйте его, и вы получите Spire.Презентация.файл jar из папки “lib”. Импортируйте файл jar в свой проект в качестве зависимости.
Способ 2. Если вы создаете проект Maven, вы можете легко добавить зависимость jar, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.presentation.free 2.6.1
Пример 1. Добавить звук к слайду
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
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\example.pptx");
//Add a shape to the first slide
Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 120, 30);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Double Click to Play Audio:");
labelShape.getTextFrame().getTextRange().setFontHeight(20);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Times New Roman"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);
//Add an audio file to the slide
Rectangle2D.Double audioRect = new Rectangle2D.Double(175, 120, 30, 30);
IAudio audio = presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:\\Users\\Administrator\\Desktop\\music.wav")).getAbsolutePath(), audioRect);
audio.setPlayMode(AudioPlayMode.ON_CLICK);
//Save to file
presentation.saveToFile("AddAudio.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Выход
Пример 2. Добавить видео на слайд
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
Presentation presentation = new Presentation();
//Load a sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\example.pptx");
//Add a shape to the first slide
Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 120, 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("Play Video:");
labelShape.getTextFrame().getTextRange().setFontHeight(20);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Times New Roman"));
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(175, 120, 400, 225);
IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:\\Users\\Administrator\\Desktop\\video.mp4")).getAbsolutePath(), videoRect);
BufferedImage coverImage = ImageIO.read( new File("C:\\Users\\Administrator\\Desktop\\coverImage.jpg"));
video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));
//Save to file
presentation.saveToFile("AddVideo.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}
Выход
Оригинал: “https://dev.to/eiceblue/how-to-add-audio-and-video-files-to-powerpoint-slides-in-java-39h1”