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();
    }
}

运行程序后,文档属性被删除。

 

(本文完)

posted @ 2019-07-09 10:14  E-iceblue  阅读(894)  评论(0编辑  收藏  举报