Link

Optimise PDF

JPedal provides a tool to optimise PDF content in various ways. Currently, the optimiser will scan the PDF for any unused objects within the PDF and create a new file with only the content used by the PDF. More options to optimise the PDF will be added in future versions. The original file is left untouched by this process unless you overwrite it with the output.

This feature is currently in preview as of version 2024.01
Preview features API are not finalised and may change. Preview features are available for users to test and provide feedback before they are released.

Optimise a PDF with the Command-Line or another language

java -cp jpedal.jar org.jpedal.tools.PdfPageDeletion 
inputFile outputFile

Optimise a PDF in Java

Static Convenience Methods

//Optimise the give PDF file with all default optimisations
PDFOptimizer.optimizePDF(new File("/path/to/input.pdf"), new File("/path/to/output.pdf"));

API Access Methods
As PDFOptimiser gains new ways to optimise a PDF the static method above will include all default options to produce an optimised version of the file.
There may be times when you only want to run a single optimisation, this can be achieved by loading a file and running each optimisation separately.

//Load the file to optimise
final AnnotInfo info = loadFile(input);

//Perform optimisations
removeUnusedObjects(info, info.pageOffsets);

//Create a byte array holding the optimised files content
final byte[] fileContent = createFileBytes(info);

//Write the optimised content to file
try (FileOutputStream fos = new FileOutputStream(output)) {
    fos.write(fileContent);
    fos.flush();
}