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—

最新文章

  1. 【详解】ERP、APS与MES系统是什么?
  2. CentOS 6.4下编译安装 gcc-4.8.0(转)
  3. phonegap 框架详解
  4. 山东省第一届ACM省赛
  5. find your present
  6. (转载)Cocos2dx-OpenGL ES2.0教程:初识MVP(3)
  7. SharePoint 命令使用集锦 (持续更新中...)
  8. MRTG开源监控安装手册
  9. sim卡中短信简要格式
  10. Light Bulb(三分)
  11. [放松一下] 经典高清电影合集 170G BT种子下载
  12. java模式:深入单例模式
  13. StrictMode使用详解
  14. js判断各个浏览器
  15. HBuilder打包Android apk 支付不了问题解决
  16. 关于ipv6被拒的问题
  17. Lucene学习笔记:基础
  18. IDEA设置
  19. Python安装第三方库的安装技巧
  20. Blender快捷键设置

热门文章

  1. [atAGC052C]Nondivisible Prefix Sums
  2. [gym102798F]Skeleton Dynamization
  3. [luogu6860]象棋与马
  4. 下一代的 3D Tiles 前瞻
  5. springboot启动流程1
  6. [IIS]发布到 IIS 上的程序没有权限访问共享文件
  7. Flink 实践教程-入门(8): 简单 ETL 作业
  8. 百胜中国使用Rainbond实现云原生落地的实践
  9. Congratulations, FYMS-OIers!
  10. 【Perl】如何安装Bioperl模块?