How to check if file is encrypted

The encryption is used to protect the file, so the only users 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 in the next example:

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.