zoukankan      html  css  js  c++  java
  • 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();
        }
    }

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

    (本文完)

  • 相关阅读:
    【maven】maven源码打包
    【分布式事务】阿里fescar
    第五章 mybatis批量更新update
    第二十九章 springboot + zipkin + mysql
    第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)
    第二十七章 springboot + zipkin(brave-okhttp实现)
    附8 zipkin
    第二十六章 hystrix-dashboard + turbine
    附7 turbine
    第二章 部署war包到tomcat
  • 原文地址:https://www.cnblogs.com/Yesi/p/11155670.html
Copyright © 2011-2022 走看看