Compress PDFs
Compress PDFs
Compress PDFs to reduce the file size prior to performing workflow operations that use bandwidth or memory.
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.compresspdf.CompressPDF45 public class CompressPDF {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDF.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 and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");23 compressPDFOperation.setInput(source);2425 // Execute the operation26 FileRef result = compressPDFOperation.execute(executionContext);2728 // Save the result at the specified location29 result.saveAs("output/compressPDFOutput.pdf");3031 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {32 LOGGER.error("Exception encountered while executing operation", ex);33 }34 }35 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CompressPDF/4// dotnet run CompressPDF.csproj56 namespace CompressPDF7 {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 and create a new operation instance.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);24 CompressPDFOperation compressPDFOperation = CompressPDFOperation.CreateNew();2526 // Set operation input from a source file.27 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"compressPDFInput.pdf");28 compressPDFOperation.SetInput(sourceFileRef);2930 // Execute the operation.31 FileRef result = compressPDFOperation.Execute(executionContext);3233 // Save the result to the specified location.34 result.SaveAs(Directory.GetCurrentDirectory() + "/output/compressPDFOutput.pdf");35 }36 catch (ServiceUsageException ex)37 {38 log.Error("Exception encountered while executing operation", ex);39 }40 // Catch more errors here . . .41 }4243 static void ConfigureLogging()44 {45 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());46 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));47 }48 }49 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/compresspdf/compress-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 credentials and create a new operation instance.15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),16 compressPDF = PDFServicesSdk.CompressPDF,17 compressPDFOperation = compressPDF.Operation.createNew();1819 // Set operation input from a source file.20 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/compressPDFInput.pdf');21 compressPDFOperation.setInput(input);2223 // Execute the operation and Save the result to the specified location.24 compressPDFOperation.execute(executionContext)25 .then(result => result.saveAsFile('output/compressPDFOutput.pdf'))26 .catch(err => {27 if(err instanceof PDFServicesSdk.Error.ServiceApiError28 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {29 console.log('Exception encountered while executing operation', err);30 } else {31 console.log('Exception encountered while executing operation', err);32 }33 });34 } catch (err) {35 console.log('Exception encountered while executing operation', err);36 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-compressPDF34curl --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 \"documentIn\": {12 \"cpf:location\": \"InputFile0\",13 \"dc:format\": \"application/pdf\"14 }15 },16 \"cpf:engine\": {17 \"repo:assetId\": \"urn:aaid:cpf:Service-f37d36f4e6724eed92149a8ff35ea061\"18 },19 \"cpf:outputs\": {20 \"documentOut\": {21 \"cpf:location\": \"multipartLabelOut\",22 \"dc:format\": \"application/pdf\"23 }24 }25}"' \26--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'
Compress PDFs with Compression Level
Compress PDFs to reduce the file size on the basis of provided
compression level, prior to performing workflow operations that use
bandwidth or memory. Refer to CompressionLevel
in the API docs for a
list of supported compression levels.
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.compresspdf.CompressPDFWithOptions45 public class CompressPDFWithOptions {6 // Initialize the logger.7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDFWithOptions.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 and create a new operation instance.18 ExecutionContext executionContext = ExecutionContext.create(credentials);19 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();2021 // Set operation input from a source file.22 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");23 compressPDFOperation.setInput(source);2425 // Build CompressPDF options from supported compression levels and set them into the operation26 CompressPDFOptions compressPDFOptions = CompressPDFOptions.compressPDFOptionsBuilder()27 .withCompressionLevel(CompressionLevel.LOW)28 .build();29 compressPDFOperation.setOptions(compressPDFOptions);3031 // Execute the operation32 FileRef result = compressPDFOperation.execute(executionContext);3334 // Save the result at the specified location35 result.saveAs("output/compressPDFWithOptionsOutput.pdf");3637 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {38 LOGGER.error("Exception encountered while executing operation", ex);39 }40 }41 }
Copied to your clipboard1// Get the samples from https://www.adobe.com/go/pdftoolsapi_net_samples2// Run the sample:3// cd CompressPDF/4// dotnet run CompressPDFWithOptions.csproj56 namespace CompressPDFWithOptions7 {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 and create a new operation instance.23 ExecutionContext executionContext = ExecutionContext.Create(credentials);24 CompressPDFOperation compressPDFOperation = CompressPDFOperation.CreateNew();2526 // Set operation input from a source file.27 FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"compressPDFInput.pdf");28 compressPDFOperation.SetInput(sourceFileRef);2930 // Build CompressPDF options from supported compression levels and set them into the operation31 CompressPDFOptions compressPDFOptions = CompressPDFOptions.CompressPDFOptionsBuilder()32 .WithCompressionLevel(CompressionLevel.LOW)33 .Build();34 compressPDFOperation.SetOptions(compressPDFOptions);3536 // Execute the operation.37 FileRef result = compressPDFOperation.Execute(executionContext);3839 // Save the result to the specified location.40 result.SaveAs(Directory.GetCurrentDirectory() + "/output/compressPDFWithOptionsOutput.pdf");41 }42 catch (ServiceUsageException ex)43 {44 log.Error("Exception encountered while executing operation", ex);45 }46 // Catch more errors here . . .47 }4849 static void ConfigureLogging()50 {51 ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());52 XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));53 }54 }55 }
Copied to your clipboard1// Get the samples from http://www.adobe.com/go/pdftoolsapi_node_sample2// Run the sample:3// node src/compresspdf/compress-pdf-with-options.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 credentials and create a new operation instance.15 const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),16 compressPDF = PDFServicesSdk.CompressPDF,17 compressPDFOperation = compressPDF.Operation.createNew();1819 // Set operation input from a source file.20 const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/compressPDFInput.pdf');21 compressPDFOperation.setInput(input);2223 // Provide any custom configuration options for the operation.24 const options = new compressPDF.options.CompressPDFOptions.Builder()25 .withCompressionLevel(PDFServicesSdk.CompressPDF.options.CompressionLevel.MEDIUM)26 .build();27 compressPDFOperation.setOptions(options);2829 // Execute the operation and Save the result to the specified location.30 compressPDFOperation.execute(executionContext)31 .then(result => result.saveAsFile('output/compressPDFWithOptionsOutput.pdf'))32 .catch(err => {33 if(err instanceof PDFServicesSdk.Error.ServiceApiError34 || err instanceof PDFServicesSdk.Error.ServiceUsageError) {35 console.log('Exception encountered while executing operation', err);36 } else {37 console.log('Exception encountered while executing operation', err);38 }39 });40 } catch (err) {41 console.log('Exception encountered while executing operation', err);42 }
Copied to your clipboard1// Please refer our REST API docs for more information2// https://documentcloud.adobe.com/document-services/index.html#post-compressPDF34curl --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 \"compressionLevel\": \"MEDIUM\"14 }15 },16 \"documentIn\": {17 \"cpf:location\": \"InputFile0\",18 \"dc:format\": \"application/pdf\"19 }20 },21 \"cpf:engine\": {22 \"repo:assetId\": \"urn:aaid:cpf:Service-f37d36f4e6724eed92149a8ff35ea061\"23 },24 \"cpf:outputs\": {25 \"documentOut\": {26 \"cpf:location\": \"multipartLabelOut\",27 \"dc:format\": \"application/pdf\"28 }29 }30}"' \31--form 'InputFile0=@"{{Placeholder for input file (absolute path)}}"'