Edit in GitHubLog an issue

Protect PDF

Protect PDFs with user password

You can password protect PDFs so that only users with a document open password can open the file.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.protectpdf.ProtectPDF
4
5 public class ProtectPDF {
6 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(ProtectPDF.class);
8
9 public static void main(String[] args) {
10
11 try {
12 // Initial setup, create credentials instance.
13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
14 .fromFile("pdfservices-api-credentials.json")
15 .build();
16
17 // Create an ExecutionContext using credentials.
18 ExecutionContext executionContext = ExecutionContext.create(credentials);
19
20 // Build ProtectPDF options by setting a User Password and Encryption
21 // Algorithm (used for encrypting the PDF file).
22 ProtectPDFOptions protectPDFOptions = ProtectPDFOptions.passwordProtectOptionsBuilder()
23 .setUserPassword("encryptPassword")
24 .setEncryptionAlgorithm(EncryptionAlgorithm.AES_256)
25 .build();
26
27 // Create a new operation instance.
28 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.createNew(protectPDFOptions);
29
30 // Set operation input from a source file.
31 FileRef source = FileRef.createFromLocalFile("src/main/resources/protectPDFInput.pdf");
32 protectPDFOperation.setInput(source);
33
34 // Execute the operation
35 FileRef result = protectPDFOperation.execute(executionContext);
36
37 // Save the result at the specified location
38 result.saveAs("output/protectPDFOutput.pdf");
39
40 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
41 LOGGER.error("Exception encountered while executing operation", ex);
42 }
43 }
44 }

Protect PDFs with owner password

You can secure a PDF file with owner/permissions password and set the restriction on certain features like printing, editing and copying in the PDF document. Refer to ContentEncryption and Permission in the API docs for a list of supported types of content to encrypt and types of document permissions.

Copied to your clipboard
1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples
2// Run the sample:
3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.protectpdf.ProtectPDFWithOwnerPassword
4
5 public class ProtectPDFWithOwnerPassword {
6
7 // Initialize the logger.
8 private static final Logger LOGGER = LoggerFactory.getLogger(ProtectPDFWithOwnerPassword.class);
9
10 public static void main(String[] args) {
11
12 try {
13 // Initial setup, create credentials instance.
14 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()
15 .fromFile("pdfservices-api-credentials.json")
16 .build();
17
18 // Create an ExecutionContext using credentials.
19 ExecutionContext executionContext = ExecutionContext.create(credentials);
20
21 // Create new permissions instance and add the required permissions
22 Permissions permissions = Permissions.createNew();
23 permissions.addPermission(Permission.PRINT_LOW_QUALITY);
24 permissions.addPermission(Permission.EDIT_DOCUMENT_ASSEMBLY);
25 permissions.addPermission(Permission.COPY_CONTENT);
26
27 // Build ProtectPDF options by setting an Owner/Permissions Password, Permissions,
28 // Encryption Algorithm (used for encrypting the PDF file) and specifying the type of content to encrypt.
29 ProtectPDFOptions protectPDFOptions = ProtectPDFOptions.passwordProtectOptionsBuilder()
30 .setOwnerPassword("password")
31 .setPermissions(permissions)
32 .setEncryptionAlgorithm(EncryptionAlgorithm.AES_256)
33 .setContentEncryption(ContentEncryption.ALL_CONTENT_EXCEPT_METADATA)
34 .build();
35
36 // Create a new operation instance.
37 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.createNew(protectPDFOptions);
38
39 // Set operation input from a source file.
40 FileRef source = FileRef.createFromLocalFile("src/main/resources/protectPDFInput.pdf");
41 protectPDFOperation.setInput(source);
42
43 // Execute the operation
44 FileRef result = protectPDFOperation.execute(executionContext);
45
46 // Save the result at the specified location
47 result.saveAs("output/protectPDFWithOwnerPasswordOutput.pdf");
48
49 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
50 LOGGER.error("Exception encountered while executing operation", ex);
51 }
52 }
53 }
54
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.