Check if a file is encrypted

The encryption protects data and allows only authorized people to open the file. So, only those who have an encryption key (such as a password) can open and view a file.

If you want to check if a file is encrypted, use the getFileInfo() method that returns the file type and flag that indicates if the file is encrypted. The following code snippet shows how to check if a file is encrypted.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.results.FileInfo;
// ...

try (Viewer viewer = new Viewer("encrypted.pdf")) {
    FileInfo fileInfo = viewer.getFileInfo();

    System.out.println("File type is: " + fileInfo.getFileType());
    System.out.println("File encrypted: " + fileInfo.isEncrypted());
}

The following image shows a sample console output:

To learn how to open an encrypted file, please refer to Load password-protected document page.