Sunday, 25 April 2021

Protect PowerPoint Presentations in Java

As we all know, unless we protect PowerPoint documents, anyone with access to the file can open, copy and edit the contents. Here I’ll introduce how to protect PowerPoint presentations with a password, set the presentation slides ready only, modify the password of encrypted file and decrypt a protected file.

It’s worth mentioning that the operations above can be realized by using Java codes without using Microsoft PowerPoint. In this tutorial, I used a free third-party library called Free Spire.Presentation for Java.

Before running Java codes, you need to add a Jar file in the library to IDEA. You can download a package from the link, unzip it and find the Jar file in the “lib” folder. Or you can refer to it by using the following configuration in Maven pom.xml.

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

Protect a PowerPoint File with a Password

The protection using password will lock your PowerPoint files. Only someone who knows the password will be able to open and edit it.

 import com.spire.presentation.*;


public class Encrypt {
public static void main(String[] args) throws Exception {
//create a Presentation object and load a PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//encrypt the document with a password
presentation.encrypt("abc123");

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

Output


Modify the Password of an Encrypted File

import com.spire.presentation.*;
public class ModifyPassword {
public static void main(String[] args) throws Exception {
//create a Presentation object and load a PowerPoint file including the original password
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\encrypt_output.pptx", "abc123");

//remove the encryption
presentation.removeEncryption();

//protect the document by setting a new password
presentation.protect("def456");

//save the resulting document
presentation.saveToFile("output/modifyPasswordOfEncryptedPPT.pptx", FileFormat.PPTX_2013);
}
}

After running Java codes above, you need to enter the new password to open and edit the PowerPoint file, or you can choose the “Read Only” to open the file but can’t edit it.

Set the Presentation Slides Read Only

import com.spire.presentation.*;
public class ReadOnly {
public static void main(String[] args) throws Exception {
//create a Presentation object and load a PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Protect the document with the password
presentation.protect("hij789");

//save the file
presentation.saveToFile("output/setDocumentReadOnly_output.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}

Output


Decrypt a Password-Protected PowerPoint File

import com.spire.presentation.*;

public class RemoveEncryption {
public static void main(String[] args) throws Exception {
//create a Presentation object and load a PowerPoint file
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\encrypt_output.pptx", "abc123");

//remove encryption.
presentation.removeEncryption();

//save the resulting document
presentation.saveToFile("output/removeEncryption.pptx", FileFormat.PPTX_2013);
}
}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...