jPDFOptimizer Developer Guide

jPDFOptimizer Developer Guide

Contents

javalogo
Introduction
Getting Started
Optimize Images
Removing Unnecessary Objects
Other Advanced Optimization Options
Distribution and JAR files

Javadoc API
Source Code Samples

Introduction

jPDFOptimizer is a Java library to optimize PDF documents. jPDFOptimizer provides the following functions:

  • Modify image resolution, compression and color spaces.
  • Detect duplicate images and fonts and merge them.
  • Remove unused objects
  • Save to the file system or to Java output streams

Like all of our libraries, jPDFOptimizer is built on top of Qoppa’s proprietary technology and so does not require any third party software or drivers.

Getting Started

// Create the PDF Optimizer object with the path of the file to optimize
pdfOptimizer = new PDFOptimizer("C:\myfile.pdf", null);
 
// Set optimization settings
OptSettings options = new OptSettings();
// ... see below for ways to set optimization settings
// ....
 
// run the optimizer and save
pdfOptimizer.optimize(options, "C:\myfile_opt.pdf");

Optimizing Images

Optimizing images is done using an ImageHandler interface.

options.setImageHandler(new MyImageHandler());

MyImageHandler extends the ImageHanlder interface which declares the convertImage() method. This method gives developers all the flexibility to optimize images based on their own criteria, on a per image basis.

public class MyImageHandler extends ImageHandler
{
// 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
if(imageInfo.isColor())
{
imageoutput.setCompression(ImageOutput.CO_JPEG2000);
imageoutput.setCompressionQuality(0.8f);
}
 
// 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.setImageHeight(newWidth);
imageoutput.setImageWidth(newHeight);
}
 
// return expected image output
return imageoutput;
}
}

Read more about PDF image optimization.

Removing Unnecessary Objects

OptSettings options = new OptSettings();
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);

Read more about optimizing a PDF by removing unnecessary objects.

Other Advanced Optimization Options

OptSettings options = new OptSettings();
options.setClearSignatures(true);
options.setFlattenAnnotations(true); /* if option to remove annotations is set, this option is ignored */
options.setFlattenFormFields(true); /* if option to remove fields is set, this option is ignored */
options.setCompressObjectsIntoStreams(true);
options.setFlateUncompressedStreams(true); 
options.setMergeDuplicateFonts(true);
options.setMergeDuplicateImages(true);

Read more about optimizing a PDF using  advanced optimization settings.

Distribution and JAR Files

Required and optional jar files for jPDFOptimizer can be found on the jPDFOptimizer Download page.

 

Javadoc API
Source Code Samples