Edit in GitHubLog an issue

Protect PDF

Secure a PDF file with a password encrypt the document. Set an owner password and restrictions on certain features like printing, editing and copying in the PDF document to prevent end users from modifying it.

Support for AES-128 and AES-256 encryption on PDF files, with granular permissions for high and low quality printing and fill and sign form field restrictions.

Rest API

See our public API Reference for 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.

Please refer the API usage guide to understand how to use our APIs.

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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/protectPDFInput.pdf").toPath())) {
12 // Initial setup, create credentials instance
13 Credentials credentials = new ServicePrincipalCredentials(
14 System.getenv("PDF_SERVICES_CLIENT_ID"),
15 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
16
17 // Creates a PDF Services instance
18 PDFServices pdfServices = new PDFServices(credentials);
19
20 // Creates an asset(s) from source file(s) and upload
21 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
22
23 // Create parameters for the job
24 ProtectPDFParams protectPDFParams = ProtectPDFParams.passwordProtectOptionsBuilder()
25 .setUserPassword("password")
26 .setEncryptionAlgorithm(EncryptionAlgorithm.AES_256)
27 .build();
28
29 // Creates a new job instance
30 ProtectPDFJob protectPDFJob = new ProtectPDFJob(asset, protectPDFParams);
31
32 // Submit the job and gets the job result
33 String location = pdfServices.submit(protectPDFJob);
34 PDFServicesResponse<ProtectPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, ProtectPDFResult.class);
35
36 // Get content from the resulting asset(s)
37 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
38 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
39
40 // Creates an output stream and copy stream asset's content to it
41 Files.createDirectories(Paths.get("output/"));
42 OutputStream outputStream = Files.newOutputStream(new File("output/protectPDFOutput.pdf").toPath());
43 LOGGER.info("Saving asset at output/protectPDFOutput.pdf");
44 IOUtils.copy(streamAsset.getInputStream(), outputStream);
45 outputStream.close();
46 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
47 LOGGER.error("Exception encountered while executing operation", ex);
48 }
49 }
50 }

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.

Please refer the API usage guide to understand how to use our APIs.

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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/protectPDFInput.pdf").toPath())) {
13 // Initial setup, create credentials instance
14 Credentials credentials = new ServicePrincipalCredentials(
15 System.getenv("PDF_SERVICES_CLIENT_ID"),
16 System.getenv("PDF_SERVICES_CLIENT_SECRET"));
17
18 // Creates a PDF Services instance
19 PDFServices pdfServices = new PDFServices(credentials);
20
21 // Creates an asset(s) from source file(s) and upload
22 Asset asset = pdfServices.upload(inputStream, PDFServicesMediaType.PDF.getMediaType());
23
24
25 // Create new permissions instance and add the required permissions
26 Permissions permissions = Permissions.createNew();
27 permissions.addPermission(Permission.PRINT_LOW_QUALITY);
28 permissions.addPermission(Permission.EDIT_DOCUMENT_ASSEMBLY);
29 permissions.addPermission(Permission.COPY_CONTENT);
30
31 // Create parameters for the job
32 ProtectPDFParams protectPDFParams = ProtectPDFParams.passwordProtectOptionsBuilder()
33 .setOwnerPassword("password")
34 .setPermissions(permissions)
35 .setEncryptionAlgorithm(EncryptionAlgorithm.AES_256)
36 .setContentEncryption(ContentEncryption.ALL_CONTENT_EXCEPT_METADATA)
37 .build();
38
39 // Creates a new job instance
40 ProtectPDFJob protectPDFJob = new ProtectPDFJob(asset, protectPDFParams);
41
42 // Submit the job and gets the job result
43 String location = pdfServices.submit(protectPDFJob);
44 PDFServicesResponse<ProtectPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, ProtectPDFResult.class);
45
46 // Get content from the resulting asset(s)
47 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
48 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
49
50 // Creates an output stream and copy stream asset's content to it
51 Files.createDirectories(Paths.get("output/"));
52 OutputStream outputStream = Files.newOutputStream(new File("output/protectPDFWithOwnerPasswordOutput.pdf").toPath());
53 LOGGER.info("Saving asset at output/protectPDFWithOwnerPasswordOutput.pdf");
54 IOUtils.copy(streamAsset.getInputStream(), outputStream);
55 outputStream.close();
56 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
57 LOGGER.error("Exception encountered while executing operation", ex);
58 }
59 }
60 }
61
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.