PDF slimming tool-Ghostscript

There are many tools for file compression or image slimming, but it is difficult to find tools for PDF file slimming, let alone calling it directly with JAVA. The optimization tool in Acrobat can compress a 150M pdf file to 1.5M with almost no discount. But the program can not directly control the functions in acrobat, even if the IAC program can be written using the SDK, it is difficult to embed it into the small client software.

 

Searching hard for available tools, there are basically no domestic ones, and there are several foreign ones, but they all have to be paid for and cannot really run in shell mode. Representative A-PDF Image Downsample, novapp, pdftk_server, verypdf-pdfcompressor, PDF-Tools.

 

Accidentally searched for Ghostscript, tried it after installation, wow, it's a bunker. The 150M file is reduced to 600K, and the experience is not bad. Keep digging and get a Ghost4j project.

 

Looks like it's really found.

 

The Ghost4j call is not smooth, and after many attempts, it can finally be compressed successfully.

 

import org.ghost4j.Ghostscript;
import org.ghost4j.GhostscriptException;
 
/**
 * Example showing how to convert a Postscript file to PDF.
 * @author Gilles Grousset ([email protected])
 */
public class PDFCompressExample {
 
     public static void main(String[] args) {
 
        //get Ghostscript instance
        Ghostscript gs = Ghostscript.getInstance();
 
        //prepare Ghostscript interpreter parameters
        //refer to Ghostscript documentation for parameter usage
        String[] gsArgs = new String[13];
        gsArgs[0] = "-dQUIET";
        gsArgs[1] = "-dNOPAUSE";
        gsArgs[2] = "-dBATCH";
        gsArgs[3] = "-dSAFER";
        gsArgs[4] = "-sDEVICE=pdfwrite";
        gsArgs[5] = "-dDownsampleColorImages=true";
        gsArgs[6] = "-dDownsampleGrayImages=true";
        gsArgs[7] = "-dDownsampleMonoImages=true";
        gsArgs[8] = "-dColorImageResolution=150";
        gsArgs[9] = "-dGrayImageResolution=150";
        gsArgs[10] = "-dMonoImageResolution=300";
       gsArgs[11] = "-sOutputFile=C:\\temp\\o3.pdf";
       gsArgs[12] = "C:\\temp\\G.pdf";

        //execute and exit interpreter
        try {
 
            gs.initialize(gsArgs);
            gs.exit();
 
        } catch (GhostscriptException e) {
            System.out.println("ERROR: " + e.getMessage());
        }
     }
 
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326973383&siteId=291194637
Recommended