Java 在Word中嵌入多媒體(視頻、音頻)文件

Word中可將Office(Word/Excel/PowerPoint)、PDF、txt等文件作爲OLE對象插入到文檔中,雙擊該對象可直接訪問或編輯該文件,除了以上常見的文件格式對象,也可以插入多媒體文件,如視頻、音頻等。本篇文章將對此作相關介紹。

 

Jar導入(2種方法)


 

1.通過 Maven 安裝

  在pom.xml中配置如下內容導入:

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>

2.手動添加jar

下載 Jar 包(Free Spire.Doc for Java)到本地,解壓,找到lib文件夾下的Spire.Doc.jar。

在IDEA中打開“Project Structure”界面,然後執行如下圖中的步驟來完成jar導入:

 

 

 

 

嵌入多媒體文件


 

代碼中嵌入多媒體文件的方法是通過調用appendOleObject(InputStream oleStream, DocPicture olePicture, String fileExtension)方法來實現,該方法中的三個參數解釋分別爲:

  • oleStream:OLE文件流
  • olePicture:用於顯示OLE對象的圖像(圖標)
  • fileExtension:嵌入的文件對象擴展名(如:mp3、mp4、avi等)

另外,該jar包中的Paragraph類提供的添加OLE對象的方法中,可通過設置不同參數,以多種方式來添加OLE對象,如圖:

 

主要代碼步驟解析:

1. 初始化Document類的一個新實例並添加一個新的節。

2. 添加段落,調用Paragraph.appendOleObject()方法將多媒體文件作爲OLE對象嵌入到段落。

3. 通過Document.saveToFile(String fileName, FileFormat fileFormat)保存文檔到指定路徑。

 

Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.fields.DocPicture;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class EmbedMediaFile {
    public static void main(String[] args)throws IOException {
        //實例化Document類的對象,並添加Section
        Document doc = new Document();
        Section section = doc.addSection();

        //定義段落樣式
        ParagraphStyle style1 = new ParagraphStyle(doc);
        style1.setName("Style");
        style1.getCharacterFormat().setFontName("Calibri");
        style1.getCharacterFormat().setFontSize(18);
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(new Color(123,104,238));
        doc.getStyles().add(style1);

        //添加段落1,嵌入視頻文件
        Paragraph para1 = section.addParagraph();
        para1.appendText("嵌入視頻文件:");
        para1.applyStyle(style1.getName());
        InputStream stream1 = new FileInputStream("Video.mp4");
        DocPicture pic1 = new DocPicture(doc);
        pic1.loadImage("logo1.png");
        para1.appendOleObject(stream1, pic1, "mp4");

        //添加一個空白段落2
        Paragraph para2 = section.addParagraph();

        //添加段落3,嵌入音頻文件
        Paragraph para3 = section.addParagraph();
        para3.appendText("嵌入音頻文件:");
        para3.applyStyle(style1.getName());
        InputStream stream2 = new FileInputStream("Audio.mp3");
        DocPicture pic2 = new DocPicture(doc);
        pic2.loadImage("logo2.png");
        para3.appendOleObject(stream2, pic2, "mp3");

        //保存文檔
        doc.saveToFile("Result.docx", FileFormat.Docx_2013);
    }
}

嵌入後的文檔效果:

 

注意事項

代碼中的所有文件路徑均爲IDEA程序的程序項目文件夾路徑,如: F:\IDEAProject\OLE_Doc\Result.docx,文件路徑可自定義爲其他路徑。

 

—End—

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章