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

運行程序後,文檔屬性被刪除。

(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章