Skip to main content

Set PDF Document Permissions

The PDF Document API allows you to restrict the operations users can perform on an encrypted PDF document.

Create an EncryptionOptions object, configure permissions, and pass the configured object to the PdfDocument.encrypt() method to encrypt the PDF document.

Use the following methods to define document permissions:

Method Description
setDataExtractionPermissions(DocumentDataExtractionPermissions value) Specifies whether users can extract document content.
setInteractivityPermissions(DocumentInteractivityPermissions value) Specifies whether users can interact with document elements.
setModificationPermissions(DocumentModificationPermissions value) Specifies whether users can modify the document.
setPrintPermissions(DocumentPrintPermissions value) Specifies whether users can print the document.

The following code snippet loads a PDF document, encrypts it, restricts specific operations, and saves the result:

import com.devexpress.docs.pdf.*;

import java.nio.file.*;
import java.nio.channels.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(channel)) {

            EncryptionOptions encryptionOptions = new EncryptionOptions(
                    "ownerPassword", 
                    "userPassword"
            ) {{

                    setDataExtractionPermissions(DocumentDataExtractionPermissions.NOT_ALLOWED);
                    setPrintPermissions(DocumentPrintPermissions.LOW_QUALITY);
                    setModificationPermissions(DocumentModificationPermissions.NOT_ALLOWED);
                    setAlgorithm(EncryptionAlgorithm.AES_256);
                }};

            pdfDocument.encrypt(encryptionOptions);

            // Save the encrypted document.
            try (WritableByteChannel writableByteChannel =
                 FileChannel.open(Path.of("Document_encrypted.pdf"),
                     StandardOpenOption.CREATE,
                     StandardOpenOption.WRITE,
                     StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}
See Also