A simple implementation of the library to open a PDF document, optimize it and save it to a new file using Qoppa’s java library jPDFOptimizer.

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import com.qoppa.pdfOptimizer.ImageHandler;
import com.qoppa.pdfOptimizer.ImageInfo;
import com.qoppa.pdfOptimizer.ImageOutput;
import com.qoppa.pdfOptimizer.OptSettings;
import com.qoppa.pdfOptimizer.PDFOptimizer;
/**
 * 
 * SimpleOptimize is a sample program to demonstrate the capabilities of 
 * Qoppa's PDF Library jPDFOptimizer. This is the simplest implementation 
 * it to open a PDF document, optimize it and save it to a new file.
 * Note that the main class SimpleOptimizer 
 * implements com.qoppa.pdfOptimizer.ImageHandler and PropertyListener
 */
public class SimpleOptimizer implements ImageHandler, PropertyChangeListener
{
    public static void main (String [] args)
    {
        try
        {
            // Load a document
            PDFOptimizer pdfOptimize = new PDFOptimizer("C:\\myfile.pdf", null);
 
            // set optimization options
            OptSettings options = new OptSettings();
 
            // Options to remove objects
            options.setDiscardAltImages(true);
            options.setDiscardAnnotations(true);
            options.setDiscardBookmarks(true);
            options.setDiscardDocumentInfo(true);
            options.setDiscardFileAttachments(true);
            options.setDiscardFormFields(true);
            options.setDiscardJSActions(true);
            options.setDiscardPageThumbnails(true);
            options.setDiscardXMPData(true);
            options.setDiscardUnusedResources(true);
            options.setDiscardLinks(true);
 
	   // Other optimizing options
	   options.setClearSignatures(true);
	   options.setFlateUncompressedStreams(true);
	   options.setMergeDuplicateFonts(true);
	   options.setMergeDuplicateImages(true);
           options.setCompressObjectsIntoStreams(true);
 
	   // create and set the image handler
	   // This class SimpleOptimizer extends ImageHandler 
	   // and implements method convertImage (see below)
           SimpleOptimizer mySimpleOptimizer = new SimpleOptimizer();
           options.setImageHandler(mySimpleOptimizer);
 
            // add property change listener to get progress notifications
            // SimpleOptimizer extends PropertyChangeListener
            pdfOptimize.addPropertyChangeListener(mySimpleOptimizer);
 
            // Save the optimized document
            pdfOptimize.optimize(options, "C:\\myfile_opt.pdf");
 
            // indicate that the file is done writing
            System.out.print("Done!");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
 
    // this is the method where you can implement any image conversion / optimization
	public ImageOutput convertImage(ImageInfo imageInfo) 
	{
		// construct an image output that by default retains the same image properties as the image input
		ImageOutput imageoutput = new ImageOutput(ImageOutput.CO_RETAIN, ImageOutput.CS_RETAIN, imageInfo.getImageWidth(), imageInfo.getImageHeight());
 
		// for gray or black and white images, use JBIG2 compressions
		if(imageInfo.isGray() || imageInfo.isMonochrome())
		{
			imageoutput.setCompression(ImageOutput.CO_JBIG2);
			imageoutput.setColorSpace(ImageOutput.CS_BW);
		}
 
		// for color images, use JPEG 2000 compression
                // and use a quality of 0.5
		if(imageInfo.isColor())
		{
			imageoutput.setCompression(ImageOutput.CO_JPEG2000);
			imageoutput.setCompressionQuality(0.5f);
		}
 
		// downgrade image resolution to 200DPI if it's higher
		if (imageInfo.getDPIX() > 200 || imageInfo.getDPIY() > 200)
		{
			// Calculate new dimensions to match DPI
			float scale = Math.min(200 / imageInfo.getDPIX(), 200 / imageInfo.getDPIY());
			int newWidth = (int)(imageInfo.getImageWidth() * scale + 0.5);
			int newHeight = (int)(imageInfo.getImageHeight() * scale + 0.5);
			imageoutput.setImageWidth(newWidth);
			imageoutput.setImageHeight(newHeight);
		}
 
		// return the new image output
		return imageoutput;
	}
 
	// write progress
	public void propertyChange(PropertyChangeEvent evt)
	{
		System.out.println("IN PROGRESS " + (String)evt.getNewValue() + "...");
	}
}

SimpleOptimizer
SimpleOptimizer
SimpleOptimizer.java
4.2 KiB
237 Downloads
Details