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("inputFile.pdf"), new File("outputFile.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 inputFile = new File("inputFile.pdf");
final File outputFile = new File("outputFile.pdf");
final PdfOptimizer optimizer = new PdfOptimizer();
//Load the file to optimize
optimizer.loadFile(inputFile);
//Optimizations to perform
optimizer.removeUnusedObjects();
//Write optimized PDF to output file
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
optimizer.writeoptimizedFileToStream(fos);
fos.flush();
}
//Close current PDF
optimizer.closePDF();