Java 添加、读取、删除PPT文档属性
文档属性是一些描述性的信息,它未包含在文件的实际内容中,但提供了有关文件的信息,可用来帮助查找和整理文件。以下示例中将介绍通过Java程序来添加PPT文档属性、读取、删除PPT文档中已有属性的方法。
使用工具:Free Spire.Presentation for Java(免费版)
Jar文件获取及导入:
方法1:下载jar文件包。解压文件后,将lib文件夹下的Spire.Presentation.jar文件导入Java程序。
方法2:可通过maven仓库导入到程序。
Java代码示例
【示例1】添加PPT文档属性
import com.spire.presentation.*; import java.sql.Date; import java.time.LocalDate; public class AddProperty { public static void main(String[]args) throws Exception { //加载测试文档 Presentation ppt = new Presentation(); ppt.loadFromFile("test.pptx"); //添加文档属性 ppt.getDocumentProperty().setAuthor("Sam"); ppt.getDocumentProperty().setManager("Danny"); ppt.getDocumentProperty().setCategory("B类"); ppt.getDocumentProperty().setCompany("E-iceblue"); ppt.getDocumentProperty().setKeywords("测试,文档,内部文档"); ppt.getDocumentProperty().setComments("仅供内部使用"); ppt.getDocumentProperty().setLastSavedBy("Jamy"); ppt.getDocumentProperty().setSubject("经贸"); ppt.getDocumentProperty().setContentStatus("可编辑"); ppt.getDocumentProperty().setLastSavedTime(new java.util.Date()); //保存 ppt.saveToFile("addproperty.pptx",FileFormat.PPTX_2010); ppt.dispose(); } }
文档属性添加效果:
【示例2】读取PPT文档属性
import com.spire.presentation.*; public class GetProperty { public static void main(String[]args) throws Exception{ //加载文档 Presentation ppt = new Presentation(); ppt.loadFromFile("addproperty.pptx"); //读取文档属性 System.out.println("标题: " + ppt.getDocumentProperty().getTitle()); System.out.println("主题: " + ppt.getDocumentProperty().getSubject()); System.out.println("作者: " + ppt.getDocumentProperty().getAuthor()); System.out.println("单位: " + ppt.getDocumentProperty().getCompany()); System.out.println("主管: " + ppt.getDocumentProperty().getManager()); System.out.println("类别: " + ppt.getDocumentProperty().getCategory()); System.out.println("关键字:" + ppt.getDocumentProperty().getKeywords()); System.out.println("备注: " + ppt.getDocumentProperty().getComments()); System.out.println("内容状态:"+ ppt.getDocumentProperty().getContentStatus()); } }
文档属性读取效果:
【示例3】删除PPT文档属性
import com.spire.presentation.*; public class RemoveProperty { public static void main(String[] args ) throws Exception{ //加载文档 Presentation ppt = new Presentation(); ppt.loadFromFile("addproperty.pptx"); //通过将对应文档属性的值设置为空来删除文档属性 ppt.getDocumentProperty().setTitle(""); ppt.getDocumentProperty().setManager(""); ppt.getDocumentProperty().setCategory(""); ppt.getDocumentProperty().setCompany(""); ppt.getDocumentProperty().setKeywords(""); ppt.getDocumentProperty().setComments(""); ppt.getDocumentProperty().setLastSavedBy(""); ppt.getDocumentProperty().setSubject(""); ppt.getDocumentProperty().setContentStatus(""); //保存 ppt.saveToFile("RemoveProperty.pptx",FileFormat.PPTX_2013); ppt.dispose(); } }
运行程序后,文档属性被删除。
(本文完)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)