PowerPoint 演示文稿的文档属性在管理和组织演示文稿方面具有很大的作用。属性中的信息,包括标题、作者、关键词等,为演示文稿提供了简明的摘要,有助于对其分类和检索,同时方便了对演示文稿的修改和维护。但有些无用的文档属性需要及时删除,以免影响对文档的管理。本文将介绍如何使用 Spire.Presentation for Java 通过 Java 程序在 PowerPoint 演示文稿中添加、获取或删除文档属性。
安装 Spire.Presentation for Java
首先,您需要在 Java 程序中添加 Spire.Presentation.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.1.3</version>
</dependency>
</dependencies>
添加文档属性到 PowerPoint 演示文稿
Spire.Presentation for Java 在 IDocumentProperty 类下提供了一系列方法,帮助用户使用 Presentation.getDocumentProperty() 方法获取演示文稿的文档属性后,再通过这一系列方法设置和获取文档的属性。为 PowerPoint 演示文稿添加文档属性的详细操作步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getDocumentProperty() 方法获取演示文稿的文档属性。
- 使用 IDocumentProperty 类下的方法设置演示文稿的属性信息。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;
public class addPresentationProperties {
public static void main(String[] args) throws Exception {
//创建一个Presentation类的对象
Presentation presentation = new Presentation();
//载入演示文稿
presentation.loadFromFile("示例.pptx");
//获取演示文稿的文档属性并设置属性信息
IDocumentProperty property = presentation.getDocumentProperty();
property.setTitle("年度经营分析报告");
property.setSubject("经营分析");
property.setAuthor("李敏");
property.setManager("吴林");
property.setCompany("冰蓝科技");
property.setCategory("报告");
property.setKeywords("经营分析; 季度经营数据; 增长数据");
property.setComments("报告已经修订定稿,无需再修改。");
//保存演示文稿
presentation.saveToFile("添加属性.pptx", FileFormat.AUTO);
presentation.dispose();
}
}
获取 PowerPoint 演示文稿的文档属性
从 PowerPoint 演示文稿中获取文档属性的具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getDocumentProperty() 方法获取演示文稿的文档属性。
- 使用 IDocumentProperty 类下的方法获取文档属性数据,并写入文本文件。
- Java
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;
import java.io.FileWriter;
public class retrievePresentationProperties {
public static void main(String[] args) throws Exception {
//创建一个Presentation类的对象
Presentation presentation = new Presentation();
//载入演示文稿
presentation.loadFromFile("添加属性.pptx");
//获取演示文稿的文档属性
IDocumentProperty property = presentation.getDocumentProperty();
//获取文档属性数据并写入文本文件
String properties = "标题:" + property.getTitle() + "\r\n"
+ "主题:" + property.getSubject() + "\r\n"
+ "作者:" + property.getAuthor() + "\r\n"
+ "主管:" + property.getManager() + "\r\n"
+ "单位:" + property.getCompany() + "\r\n"
+ "类别:" + property.getCategory() + "\r\n"
+ "关键词:" + property.getKeywords() + "\r\n"
+ "备注:" + property.getComments();
FileWriter presentationProperties = new FileWriter("获取属性.txt");
presentationProperties.write(properties);
presentationProperties.close();
}
}
删除 PowerPoint 演示文稿的文档属性
删除 PowerPoint 演示文稿文档属性可通过将属性设置为空白来实现。详细操作步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.loadFromFile() 方法载入演示文稿。
- 使用 Presentation.getDocumentProperty() 方法获取演示文稿的文档属性。
- 使用 IDocumentProperty 类下的方法将文档属性设置为空值。
- 使用 Presentation.saveToFile() 方法保存演示文稿。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;
public class deletePresentationProperties {
public static void main(String[] args) throws Exception {
//创建一个Presentation类的对象
Presentation presentation = new Presentation();
//载入演示文稿
presentation.loadFromFile("添加属性.pptx");
//获取演示文稿的文档属性并将属性设置为空值
IDocumentProperty property = presentation.getDocumentProperty();
property.setTitle(" ");
property.setSubject("");
property.setAuthor("");
property.setManager("");
property.setCompany("");
property.setCategory("");
property.setKeywords("");
property.setComments("");
//保存演示文稿
presentation.saveToFile("删除属性.pptx", FileFormat.AUTO);
presentation.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。