JPedal provides a tool to optimize PDF content in various ways. The optimizer has the ability to remove unused content from the provided PDF, reducing its file size. The original file is left untouched by this process unless you overwrite it with the output.
Optimize a PDF with the Command-Line or another language
java -cp jpedal.jar org.jpedal.tools.PdfOptimizer inputFile outputFile
Optimize a PDF in Java
Static Convenience Methods
//Optimize the give PDF file with all default optimizations
PdfOptimizer.optimizePDF(new File("/path/to/input.pdf"), new File("/path/to/output.pdf"));
API Access Methods
As PDFOptimizer gains new ways to optimize a PDF the static method above will include all default options to produce an optimized version of the file.
There may be times when you only want to run a single optimization, this can be achieved by loading a file and running each optimization separately.
final File input = new File("path/to/input.pdf");
final File output = new File("path/to/output.pdf");
final PdfOptimizer optimizer = new PdfOptimizer();
//Load the file to optimize
optimizer.loadFile(input);
//Optimizations to perform
optimizer.removeUnusedObjects();
//Write optimized PDF to output file
try (FileOutputStream fos = new FileOutputStream(output)) {
optimizer.writeoptimizedFileToStream(fos);
fos.flush();
}
//Close current PDF
optimizer.closePDF();