Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Document properties of PowerPoint presentations hold immense value in managing and organizing presentations. The information in properties, including title, author, keywords, etc., provides a concise summary, aids in categorization and searchability, and contributes to maintaining a comprehensive presentation history. However, some useless document properties need to be deleted to prevent them from affecting document management. This article is going to show how to add, retrieve, or delete document properties in PowerPoint presentations using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.2.8</version>
    </dependency>
</dependencies>
    

Add Document Properties to a PowerPoint Presentation

Spire.Presentation for Java provides a set of methods under IDocumentProperty class to enable users to set and retrieve document properties of a presentation after getting the properties using the Presentation.getDocumentProperty() method. The detailed steps for adding document properties to a PowerPoint presentation are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the document properties of the presentation using Presentation.getDocumentProperty() method.
  • Set the document properties using methods under IDocumentProperty class.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

public class addPresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a PowerPoint presentation
        presentation.loadFromFile("Sample.pptx");

        //Get the properties of the presentation and add items
        IDocumentProperty property = presentation.getDocumentProperty();
        property.setTitle("Annual Business Analysis Report");
        property.setSubject("Business Analysis");
        property.setAuthor("Taylor");
        property.setManager("John");
        property.setCompany("E-iceblue");
        property.setCategory("Report");
        property.setKeywords("Operating Analysis; Quarterly Operating Data; Growth");
        property.setComments("The report has been revised and finalized and does not require further consultation.");

        //Save the presentation file
        presentation.saveToFile("AddProperties.pptx", FileFormat.AUTO);
        presentation.dispose();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Retrieve Document Properties from a PowerPoint Presentation

The detailed steps for retrieving document properties from a PowerPoint presentation are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the properties of the presentation using Presentation.getDocumentProperty() method.
  • Retrieve property information using methods under IDocumentProperty class and write it to a text file.
  • Java
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

import java.io.FileWriter;

public class retrievePresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a presentation file
        presentation.loadFromFile("AddProperties.pptx");

        //Get the properties of the presentation
        IDocumentProperty property = presentation.getDocumentProperty();

        //Get the properties and write them to a text file
        String properties = "Title: " + property.getTitle() + "\r\n"
                + "Subject: " + property.getSubject() + "\r\n"
                + "Author: " + property.getAuthor() + "\r\n"
                + "Manager: " + property.getManager() + "\r\n"
                + "Company: " + property.getCompany() + "\r\n"
                + "Category: " + property.getCategory() + "\r\n"
                + "Keywords: " + property.getKeywords() + "\r\n"
                + "Comments: " + property.getComments();
        FileWriter presentationProperties = new FileWriter("PresentationProperties.txt");
        presentationProperties.write(properties);
        presentationProperties.close();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Delete Document Properties of a PowerPoint Presentation

Removing document properties from a presentation is similar to adding properties. Just set the property data to null and the document properties can be removed. The details are as follows:

  • Create an object of Presentation class.
  • Load a presentation file using Presentation.loadFromFile() method.
  • Get the document properties of the presentation using Presentation.getDocumentProperty() method.
  • Set the document properties to null using methods under IDocumentProperty class.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.IDocumentProperty;
import com.spire.presentation.Presentation;

public class deletePresentationProperties {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation presentation = new Presentation();

        //Load a PowerPoint presentation
        presentation.loadFromFile("AddProperties.pptx");

        //Get the properties of the presentation and set the properties to null
        IDocumentProperty property = presentation.getDocumentProperty();
        property.setTitle("");
        property.setSubject("");
        property.setAuthor("");
        property.setManager("");
        property.setCompany("");
        property.setCategory("");
        property.setKeywords("");
        property.setComments("");

        //Save the presentation file
        presentation.saveToFile("DeleteProperties.pptx", FileFormat.AUTO);
        presentation.dispose();
    }
}

Java: Set, Retrieve, or Delete Document Properties in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.