Set and Get Document Properties in PowerPoint in Java

This article demonstrates how to set and get document properties in PowerPoint using Spire.Presentation for Java.

Set Document Properties

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class SetAndGetDocumentProperties {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

        //load a PowerPoint document
        presentation.loadFromFile("example.pptx");

        //set document properties for the PowerPoint document
        presentation.getDocumentProperty().setApplication("Spire.Presentation");
        presentation.getDocumentProperty().setAuthor("E-iceblue");
        presentation.getDocumentProperty().setCompany("E-iceblue Co., Ltd.");
        presentation.getDocumentProperty().setKeywords("Demo File");
        presentation.getDocumentProperty().setComments("For internal use only.");
        presentation.getDocumentProperty().setCategory("Demo");
        presentation.getDocumentProperty().setTitle("This is a demo file.");
        presentation.getDocumentProperty().setSubject("Test");

        //save the document
        presentation.saveToFile("addProperties.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Set and Get Document Properties in PowerPoint in Java

Get Document Properties

import com.spire.presentation.Presentation;

import java.io.FileWriter;
import java.io.IOException;

public class GetDocumentProperties {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

        //load a PowerPoint document
        presentation.loadFromFile("addProperties.pptx");

        //get the document properties of the PowerPoint document
        String application = presentation.getDocumentProperty().getApplication();
        String author = presentation.getDocumentProperty().getAuthor();
        String company = presentation.getDocumentProperty().getCompany();
        String keywords = presentation.getDocumentProperty().getKeywords();
        String comments = presentation.getDocumentProperty().getComments();
        String category = presentation.getDocumentProperty().getCategory();
        String title = presentation.getDocumentProperty().getTitle();
        String subject = presentation.getDocumentProperty().getSubject();

        //Create a StringBuilder to save the document properties
        StringBuilder content = new StringBuilder();
        content.append("DocumentProperty.Application: " + application);
        content.append("\r\nDocumentProperty.Author: " + author);
        content.append("\r\nDocumentProperty.Company " + company);
        content.append("\r\nDocumentProperty.Keywords: " + keywords);
        content.append("\r\nDocumentProperty.Comments: " + comments);
        content.append("\r\nDocumentProperty.Category: " + category);
        content.append("\r\nDocumentProperty.Title: " + title);
        content.append("\r\nDocumentProperty.Subject: " + subject);

        //save to a .txt document
        writeStringToTxt(content.toString(),"getProperties.txt");
    }
    public static void writeStringToTxt(String content, String txtFileName) throws IOException {
        FileWriter fWriter= new FileWriter(txtFileName,true);
        try {
            fWriter.write(content);
        }catch(IOException ex){
            ex.printStackTrace();
        }finally{
            try{
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Set and Get Document Properties in PowerPoint in Java