Optimize PDF Files for Storage - Java

This tutorial shows how to quickly and efficiently reduce the size of PDF files while preserving as much of the original file's quality as possible in a Java application using the LEADTOOLS SDK.

Summary This tutorial covers how to efficiently reduce the size of PDF files in a Java Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Optimize PDF Files for Storage - Java tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS22\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the PDF Optimizer Code

With the project created, the references added, and the license set, coding can begin.

In the Package Explorer, open the _Main.java class and the following statements to the import block at the top.

import java.io.IOException; 
import java.nio.file.*; 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.pdf.*; 

Add a new method to the _Main class named OptimizeFiles(String _pdfFileDir). Call this method inside the run() method, beneath the call to SetLicense(). The string passed into the OptimizeFiles() method will be the directory pointing to the PDF files you wish to optimize. For the purposes of this tutorial, the following directory will be used: C:\LEADTOOLS22\Resources\Images

Add the below code to the new method to create the PDFOptimizerOptions object with the properties set to optimize the PDFs in the given directory.

void OptimizeFiles(String _pdfFileDir) { 
    RasterCodecs _codecs = new RasterCodecs(); 
    // Set the path to the PDF utilities dll 
    File pdfFileDir = new File(_pdfFileDir); 
    FilenameFilter pdfFilter = new FilenameFilter() { 
        public boolean accept(File dir, String name) { 
            return name.toLowerCase().endsWith(".pdf"); 
    File[] _pdfFiles = pdfFileDir.listFiles(pdfFilter); 
    File _destDir = new File(pdfFileDir, "OptimizedPDFs"); 
    if (!_destDir.exists()) 
    PDFOptimizerOptions myOptimizerOptions = new PDFOptimizerOptions(); 
    for (File _pdfFile : _pdfFiles) 
        // Get filename without extension 
        String _pdfFilename = _pdfFile.getName(); 
        int pos = _pdfFile.getName().lastIndexOf("."); 
        if (pos > 0 && pos < (_pdfFilename.length() - 1)) 
             _pdfFilename =  _pdfFilename.substring(0, pos); 
        File destFile = new File(_destDir, _pdfFilename + "_OPTIMIZED.pdf"); 
        CodecsImageInfo _info = _codecs.getInformation(_pdfFile.toString(), true); 
        long _beforeBytes = _info.getSizeDisk(); 
        System.out.println("Size of " + _pdfFile.toString() + ": " + _beforeBytes); 
        PDFFile _pdf = new PDFFile(_pdfFile.toString()); 
        System.out.println("PDF optimized and saved to " + destFile.toString()); 
        CodecsImageInfo _afterInfo = _codecs.getInformation(destFile.toString(), true); 
        long _afterBytes = _afterInfo.getSizeDisk(); 
        System.out.println("Size of optimized " + _pdfFile.toString() + ": " + _afterBytes); 


The AutoOptimizerMode property sets the enumeration that indicates how you want to optimize your PDF files (BEST_QUALITY, BEST_SIZE, AVERAGE_SIZE, etc.).

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application grabs all the PDF files in the given directory, optimizes the files based on the PDFOptimizerOptions, and exports the PDF to the created directory.

Screenshot showing the optimize results.


This tutorial showed how to add the necessary references to optimize each PDF file in a given directory for storage. Also, we covered how to work with the PDFFile and PDFOptimizerOptions classes.

See Also

Help Version 22.0.2023.9.28
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.