Skip to content
DemoSampleAPI ReferenceFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Android Guides Compress

PDF Compression ​

CPDFCompressDialog in ComPDFKit_Tools provides document selection, quality presets, custom options, an output directory selector, and progress UI.

Show the Dialog ​

The dialog manages the supplied document and closes it when destroyed. Use a separate instance rather than the one currently being read or edited. This example runs in an Activity; pdfPath is the input file path:

java
CPDFDocument compressionDocument = new CPDFDocument(this);
if (compressionDocument.open(pdfPath)
        == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
    CPDFCompressDialog dialog = new CPDFCompressDialog();
    dialog.setDocument(compressionDocument);
    dialog.setCompressDocumentListener((success, path, uri) -> {
        if (success) {
            // Open the output using path or uri.
        } else {
            // Compression, saving, or cancellation produced no successful result.
        }
    });
    dialog.show(getSupportFragmentManager(), "compress");
} else {
    compressionDocument.close();
}
kotlin
val compressionDocument = CPDFDocument(this)
if (compressionDocument.open(pdfPath) ==
    CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
    val dialog = CPDFCompressDialog()
    dialog.setDocument(compressionDocument)
    dialog.setCompressDocumentListener { success, path, uri ->
        if (success) {
            // Open the output using path or uri.
        } else {
            // Handle the unsuccessful operation.
        }
    }
    dialog.show(supportFragmentManager, "compress")
} else {
    compressionDocument.close()
}

CPDFCompressDialog is in com.compdfkit.tools.compress; CPDFDocument is in com.compdfkit.core.document.

Choose Compression Options ​

PresetDownsample threshold / target PPIImage quality
Low120 / 9630
Standard180 / 15045
High220 / 15075
CustomSet in custom optionsSet in custom options

Presets control image re-encoding and sampling. Output size depends on the source; preset names do not guarantee a fixed compression ratio or visual quality.

After selecting Custom, open CPDFCustomCompressOptionsDialog to configure quality, PPI, embedded fonts, and cleanup rules. The downsample threshold must exceed the target PPI.

Cleanup can remove form actions, JavaScript, thumbnails, bookmarks, annotations and forms, attachments, document information, and layers. Removing fonts or business content can change rendering and functionality. Review the options before saving.

Output and Cancellation ​

The dialog optimizes on a background thread and displays progress. Cancelling or closing it requests a stop. setCompressDocumentListener() receives:

ParameterDescription
successWhether optimization and output succeeded
pathA path for opening the successful output
uriThe Uri for public-directory output; use the actual returned value

Use the output path or Uri only when success is true. Successful compression closes the supplied document; reopen the output for further operations. Do not use incomplete output after failure or cancellation.

For algorithm configuration, color conversion, or detailed results, see Compress and Optimize PDF Files.