PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
CPDFCompressDialog in ComPDFKit_Tools provides document selection, quality presets, custom options, an output directory selector, and progress UI.
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:
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();
}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.
| Preset | Downsample threshold / target PPI | Image quality |
|---|---|---|
| Low | 120 / 96 | 30 |
| Standard | 180 / 150 | 45 |
| High | 220 / 150 | 75 |
| Custom | Set in custom options | Set 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.
The dialog optimizes on a background thread and displays progress. Cancelling or closing it requests a stop. setCompressDocumentListener() receives:
| Parameter | Description |
|---|---|
success | Whether optimization and output succeeded |
path | A path for opening the successful output |
uri | The 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.