Edit in GitHubLog an issue

Compress PDFs

Compress PDFs

Compress PDFs to reduce the file size prior to performing workflow operations that use bandwidth or memory.

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.compresspdf.CompressPDF
4
5 public class CompressPDF {
6 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDF.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 and create a new operation instance.
18 ExecutionContext executionContext = ExecutionContext.create(credentials);
19 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");
23 compressPDFOperation.setInput(source);
24
25 // Execute the operation
26 FileRef result = compressPDFOperation.execute(executionContext);
27
28 // Save the result at the specified location
29 result.saveAs("output/compressPDFOutput.pdf");
30
31 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
32 LOGGER.error("Exception encountered while executing operation", ex);
33 }
34 }
35 }

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.

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.compresspdf.CompressPDFWithOptions
4
5 public class CompressPDFWithOptions {
6 // Initialize the logger.
7 private static final Logger LOGGER = LoggerFactory.getLogger(CompressPDFWithOptions.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 and create a new operation instance.
18 ExecutionContext executionContext = ExecutionContext.create(credentials);
19 CompressPDFOperation compressPDFOperation = CompressPDFOperation.createNew();
20
21 // Set operation input from a source file.
22 FileRef source = FileRef.createFromLocalFile("src/main/resources/compressPDFInput.pdf");
23 compressPDFOperation.setInput(source);
24
25 // Build CompressPDF options from supported compression levels and set them into the operation
26 CompressPDFOptions compressPDFOptions = CompressPDFOptions.compressPDFOptionsBuilder()
27 .withCompressionLevel(CompressionLevel.LOW)
28 .build();
29 compressPDFOperation.setOptions(compressPDFOptions);
30
31 // Execute the operation
32 FileRef result = compressPDFOperation.execute(executionContext);
33
34 // Save the result at the specified location
35 result.saveAs("output/compressPDFWithOptionsOutput.pdf");
36
37 } catch (ServiceApiException | IOException | SdkException | ServiceUsageException ex) {
38 LOGGER.error("Exception encountered while executing operation", ex);
39 }
40 }
41 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.