Edit in GitHubLog an issue

Compress PDFs

Reduce the size of PDF files by compressing to smaller sizes for lower bandwidth viewing, downloading, and sharing.

Support for multiple compression levels to retain the quality of images and graphics

REST API

See our public API Reference for Compress PDF

Compress PDFs

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

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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/compressPDFInput.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 // Creates a new job instance
24 CompressPDFJob compressPDFJob = new CompressPDFJob(asset);
25
26 // Submit the job and gets the job result
27 String location = pdfServices.submit(compressPDFJob);
28 PDFServicesResponse<CompressPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CompressPDFResult.class);
29
30 // Get content from the resulting asset(s)
31 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
32 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
33
34 // Creating an output stream and copying stream asset content to it
35 Files.createDirectories(Paths.get("output/"));
36 OutputStream outputStream = Files.newOutputStream(new File("output/compressPDFOutput.pdf").toPath());
37 LOGGER.info("Saving asset at output/compressPDFOutput.pdf");
38 IOUtils.copy(streamAsset.getInputStream(), outputStream);
39 outputStream.close();
40 } catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
41 LOGGER.error("Exception encountered while executing operation", ex);
42 }
43 }
44 }

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.

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.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 (InputStream inputStream = Files.newInputStream(new File("src/main/resources/compressPDFInput.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 CompressPDFParams compressPDFParams = CompressPDFParams.compressPDFParamsBuilder()
25 .withCompressionLevel(CompressionLevel.LOW)
26 .build();
27
28 // Creates a new job instance
29 CompressPDFJob compressPDFJob = new CompressPDFJob(asset)
30 .setParams(compressPDFParams);
31
32 // Submit the job and gets the job result
33 String location = pdfServices.submit(compressPDFJob);
34 PDFServicesResponse<CompressPDFResult> pdfServicesResponse = pdfServices.getJobResult(location, CompressPDFResult.class);
35
36 // Get content from the resulting asset(s)
37 Asset resultAsset = pdfServicesResponse.getResult().getAsset();
38 StreamAsset streamAsset = pdfServices.getContent(resultAsset);
39
40 // Creating an output stream and copying stream asset content to it
41 Files.createDirectories(Paths.get("output/"));
42 OutputStream outputStream = Files.newOutputStream(new File("output/compressPDFWithOptionsOutput.pdf").toPath());
43 LOGGER.info("Saving asset at output/compressPDFWithOptionsOutput.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 }
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.