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.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.protectpdf.ProtectPDF45 public class ProtectPDF {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(ProtectPDF.class);89 public static void main(String[] args) {1011 try {12 // Initial setup, create credentials instance.13 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()14 .fromFile("pdfservices-api-credentials.json")15 .build();1617 // Create an ExecutionContext using credentials.18 ExecutionContext executionContext = ExecutionContext.create(credentials);1920 // Build ProtectPDF options by setting a User Password and Encryption21 // Algorithm (used for encrypting the PDF file).22 ProtectPDFOptions protectPDFOptions = ProtectPDFOptions.passwordProtectOptionsBuilder()23 .setUserPassword("encryptPassword")24 .setEncryptionAlgorithm(EncryptionAlgorithm.AES_256)25 .build();2627 // Create a new operation instance.28 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.createNew(protectPDFOptions);2930 // Set operation input from a source file.31 FileRef source = FileRef.createFromLocalFile("src/main/resources/protectPDFInput.pdf");32 protectPDFOperation.setInput(source);3334 // Execute the operation35 FileRef result = protectPDFOperation.execute(executionContext);3637 // Save the result at the specified location38 result.saveAs("output/protectPDFOutput.pdf");3940 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {41 LOGGER.error("Exception encountered while executing operation", ex);42 }43 }44 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd ProtectPDF/4// dotnet run ProtectPDF.csproj56 namespace ProtectPDF7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Build ProtectPDF options by setting a User Password and Encryption26 // Algorithm (used for encrypting the PDF file).27 ProtectPDFOptions protectPDFOptions = ProtectPDFOptions.PasswordProtectOptionsBuilder()28 .SetUserPassword("encryptPassword")29 .SetEncryptionAlgorithm(EncryptionAlgorithm.AES_256)30 .Build();3132 // Create a new operation instance33 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.CreateNew(protectPDFOptions);3435 // Set operation input from a source file.36 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"protectPDFInput.pdf");37 protectPDFOperation.SetInput(sourceFileRef);3839 // Execute the operation.40 FileRef result = protectPDFOperation.Execute(executionContext);4142 // Save the result to the specified location.43 result.SaveAs(Directory.GetCurrentDirectory() + "/output/protectPDFOutput.pdf");44 }45 catch (ServiceUsageException ex)46 {47 log.Error("Exception encountered while executing operation", ex);48 }49 // Catch more errors here . . .50 }5152 static void ConfigureLogging()53 {54 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());55 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));56 }57 }58 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/protectpdf/protect-pdf.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);1617 // Build ProtectPDF options by setting a User Password and Encryption18 // Algorithm (used for encrypting the PDF file).19 const protectPDF = PDFServicesSdk.ProtectPDF,20 options = new protectPDF.options.PasswordProtectOptions.Builder()21 .setUserPassword("encryptPassword")22 .setEncryptionAlgorithm(PDFServicesSdk.ProtectPDF.options.EncryptionAlgorithm.AES_256)23 .build();2425 // Create a new operation instance.26 const protectPDFOperation = protectPDF.Operation.createNew(options);2728 // Set operation input from a source file.29 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/protectPDFInput.pdf');30 protectPDFOperation.setInput(input);3132 // Execute the operation and Save the result to the specified location.33 protectPDFOperation.execute(executionContext)34 .then(result => result.saveAsFile('output/protectPDFOutput.pdf'))35 .catch(err => {36 if(err instanceof PDFServicesSdk.Error.ServiceApiError37 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {38 console.log('Exception encountered while executing operation', err);39 } else {40 console.log('Exception encountered while executing operation', err);41 }42 });43 } catch (err) {44 console.log('Exception encountered while executing operation', err);45 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-protectPDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"params\": {12 \"cpf:inline\": {13 \"passwordProtection\": {14 \"userPassword\": \"user_password\",15 },16 \"encryptionAlgorithm\": \"AES_128\"17 }18 },19 \"documentIn\": {20 \"cpf:location\": \"InputFile0\",21 \"dc:format\": \"application/pdf\"22 }23 },24 \"cpf:engine\": {25 \"repo:assetId\": \"urn:aaid:cpf:Service-627aa455ac5d4c338ad49ca882e0fce5\"26 },27 \"cpf:outputs\": {28 \"documentOut\": {29 \"cpf:location\": \"multipartLabelOut\",30 \"dc:format\": \"application/pdf\"31 }32 }33}"' \34--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'
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.
Java
.NET
Node JS
REST API
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_java_samples2// Run the sample:3// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.protectpdf.ProtectPDFWithOwnerPassword45 public class ProtectPDFWithOwnerPassword {67 // Initialize the logger.8 private static final Logger LOGGER = LoggerFactory.getLogger(ProtectPDFWithOwnerPassword.class);910 public static void main(String[] args) {1112 try {13 // Initial setup, create credentials instance.14 Credentials credentials = Credentials.serviceAccountCredentialsBuilder()15 .fromFile("pdfservices-api-credentials.json")16 .build();1718 // Create an ExecutionContext using credentials.19 ExecutionContext executionContext = ExecutionContext.create(credentials);2021 // Create new permissions instance and add the required permissions22 Permissions permissions = Permissions.createNew();23 permissions.addPermission(Permission.PRINT_LOW_QUALITY);24 permissions.addPermission(Permission.EDIT_DOCUMENT_ASSEMBLY);25 permissions.addPermission(Permission.COPY_CONTENT);2627 // 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();3536 // Create a new operation instance.37 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.createNew(protectPDFOptions);3839 // Set operation input from a source file.40 FileRef source = FileRef.createFromLocalFile("src/main/resources/protectPDFInput.pdf");41 protectPDFOperation.setInput(source);4243 // Execute the operation44 FileRef result = protectPDFOperation.execute(executionContext);4546 // Save the result at the specified location47 result.saveAs("output/protectPDFWithOwnerPasswordOutput.pdf");4849 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {50 LOGGER.error("Exception encountered while executing operation", ex);51 }52 }53 }54
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd ProtectPDFWithOwnerPassword/4// dotnet run ProtectPDFWithOwnerPassword.csproj56 namespace ProtectPDFWithOwnerPassword7 {8 class Program9 {10 private static readonly ILog log = LogManager.GetLogger(typeof(Program));11 static void Main()12 {13 //Configure the logging14 ConfigureLogging();15 try16 {17 // Initial setup, create credentials instance.18 Credentials credentials = Credentials.ServiceAccountCredentialsBuilder()19 .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json")20 .Build();2122 // Create an ExecutionContext using credentials.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);2425 // Create new permissions instance and add the required permissions26 Permissions permissions = Permissions.CreateNew();27 permissions.AddPermission(Permission.PRINT_LOW_QUALITY);28 permissions.AddPermission(Permission.EDIT_DOCUMENT_ASSEMBLY);29 permissions.AddPermission(Permission.COPY_CONTENT);3031 // Build ProtectPDF options by setting an Owner/Permissions Password, Permissions,32 // Encryption Algorithm (used for encrypting the PDF file) and specifying the type of content to encrypt.33 ProtectPDFOptions protectPDFOptions = ProtectPDFOptions.PasswordProtectOptionsBuilder()34 .SetOwnerPassword("password")35 .SetPermissions(permissions)36 .SetEncryptionAlgorithm(EncryptionAlgorithm.AES_256)37 .SetContentEncryption(ContentEncryption.ALL_CONTENT_EXCEPT_METADATA)38 .Build();3940 // Create a new operation instance41 ProtectPDFOperation protectPDFOperation = ProtectPDFOperation.CreateNew(protectPDFOptions);4243 // Set operation input from a source file.44 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"protectPDFInput.pdf");45 protectPDFOperation.SetInput(sourceFileRef);4647 // Execute the operation.48 FileRef result = protectPDFOperation.Execute(executionContext);4950 // Save the result to the specified location.51 result.SaveAs(Directory.GetCurrentDirectory() + "/output/protectPDFWithOwnerPasswordOutput.pdf");52 }53 catch (ServiceUsageException ex)54 {55 log.Error("Exception encountered while executing operation", ex);56 }57 // Catch more errors here . . .58 }5960 static void ConfigureLogging()61 {62 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());63 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));64 }65 }66 }67
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/protectpdf/protect-pdf-with-owner-password.js45 const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');67 try {8 // Initial setup, create credentials instance.9 const credentials = PDFServicesSdk.Credentials10 .serviceAccountCredentialsBuilder()11 .fromFile("pdfservices-api-credentials.json")12 .build();1314 // Create an ExecutionContext using credentials15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials);1617 // Create new permissions instance and add the required permissions18 const protectPDF = PDFServicesSdk.ProtectPDF,19 protectPDFOptions = protectPDF.options,20 permissions = protectPDFOptions.Permissions.createNew();21 permissions.addPermission(protectPDFOptions.Permission.PRINT_LOW_QUALITY);22 permissions.addPermission(protectPDFOptions.Permission.EDIT_DOCUMENT_ASSEMBLY);23 permissions.addPermission(protectPDFOptions.Permission.COPY_CONTENT);2425 // Build ProtectPDF options by setting an Owner/Permissions Password, Permissions,26 // Encryption Algorithm (used for encrypting the PDF file) and specifying the type of content to encrypt.27 const options = new protectPDFOptions.PasswordProtectOptions.Builder()28 .setOwnerPassword("password")29 .setPermissions(permissions)30 .setEncryptionAlgorithm(protectPDFOptions.EncryptionAlgorithm.AES_256)31 .setContentEncryption(protectPDFOptions.ContentEncryption.ALL_CONTENT_EXCEPT_METADATA)32 .build();3334 // Create a new operation instance.35 const protectPDFOperation = protectPDF.Operation.createNew(options);3637 // Set operation input from a source file.38 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/protectPDFInput.pdf');39 protectPDFOperation.setInput(input);4041 // Execute the operation and Save the result to the specified location.42 protectPDFOperation.execute(executionContext)43 .then(result => result.saveAsFile('output/protectPDFWithOwnerPasswordOutput.pdf'))44 .catch(err => {45 if(err instanceof PDFServicesSdk.Error.ServiceApiError46 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {47 console.log('Exception encountered while executing operation', err);48 } else {49 console.log('Exception encountered while executing operation', err);50 }51 });52 } catch (err) {53 console.log('Exception encountered while executing operation', err);54 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-protectPDF34curl --location --request POST 'https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D' \5--header 'Authorization: Bearer {{Placeholder for token}}' \6--header 'Accept: application/json, text/plain, */*' \7--header 'x-api-key: {{Placeholder for client_id}}' \8--header 'Prefer: respond-async,wait=0' \9--form 'contentAnalyzerRequests="{10 \"cpf:inputs\": {11 \"params\": {12 \"cpf:inline\": {13 \"passwordProtection\": {14 \"ownerPassword\": \"owner_password\"15 },16 \"permissions\": [\"PRINT_LOW_QUALITY\", \"PRINT_HIGH_QUALITY\", \"EDIT_CONTENT\", \"EDIT_FILL_AND_SIGN_FORM_FIELDS\", \"EDIT_ANNOTATIONS\", \"EDIT_DOCUMENT_ASSEMBLY\", \"COPY_CONTENT\"],17 \"contentToEncrypt\": \"ALL_CONTENT_EXCEPT_METADATA\",18 \"encryptionAlgorithm\": \"AES_128\"19 }20 },21 \"documentIn\": {22 \"cpf:location\": \"InputFile0\",23 \"dc:format\": \"application/pdf\"24 }25 },26 \"cpf:engine\": {27 \"repo:assetId\": \"urn:aaid:cpf:Service-627aa455ac5d4c338ad49ca882e0fce5\"28 },29 \"cpf:outputs\": {30 \"documentOut\": {31 \"cpf:location\": \"multipartLabelOut\",32 \"dc:format\": \"application/pdf\"33 }34 }35}"' \36--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'