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.

Overview  
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:

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.

Java
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: <INSTALL_DIR>\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.

Java
void OptimizeFiles(String _pdfFileDir) { 
    RasterCodecs _codecs = new RasterCodecs(); 
    // Set the path to the PDF utilities dll 
    _codecs.getOptions().getPdf().setInitialPath("C:\\LEADTOOLS22\\Bin\\CDLL\\x64"); 
 
    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()) 
        _destDir.mkdir(); 
 
    PDFOptimizerOptions myOptimizerOptions = new PDFOptimizerOptions(); 
    myOptimizerOptions.setAutoOptimizerMode(PDFAutoOptimizerMode.BEST_SIZE); 
     
    myOptimizerOptions.setColorImageDownsamplingMode(PDFDownsamplingMode.AVERAGE); 
    myOptimizerOptions.setGrayImageDownsamplingMode(PDFDownsamplingMode.BICUBIC); 
    myOptimizerOptions.setMonoImageDownsamplingMode(PDFDownsamplingMode.BICUBIC); 
     
    myOptimizerOptions.setColorImageDownsampleFactor(2.0); 
    myOptimizerOptions.setGrayImageDownsampleFactor(2.0); 
    myOptimizerOptions.setMonoImageDownsampleFactor(2.0); 
     
    myOptimizerOptions.setColorImageDPI(150); 
    myOptimizerOptions.setGrayImageDPI(150); 
    myOptimizerOptions.setMonoImageDPI(150); 
     
    myOptimizerOptions.setColorImageCompression(RasterImageFormat.JPEG); 
    myOptimizerOptions.setGrayImageCompression(RasterImageFormat.RAS_PDF_LZW); 
    myOptimizerOptions.setMonoImageCompression(RasterImageFormat.JBIG); 
     
    myOptimizerOptions.setEmbedAllFonts(false);  
    myOptimizerOptions.setSubsetFonts(true); 
     
    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()); 
        _pdf.setOptimizerOptions(myOptimizerOptions); 
        _pdf.optimize(destFile.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); 
    } 
    _codecs.dispose(); 
} 

Note

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.

Wrap-up

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.2024.3.20
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.