How to check if file is encrypted

The encryption is used to protect the file, so the only people with an encryption key (such as a password) can open and view a file. In case you want to check if a file is encrypted, you can use getFileInfo() method that returns file type and flag that indicates if the file is encrypted as it is shown below.)

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

After running the code above you will see an output like this:

File type is: Portable Document Format File (.pdf)
File encrypted: true

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

More resources

Advanced Usage Topics

To learn more about document viewing features, please refer to the advanced usage section.

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.