Optimize PDF Documents
- 4 minutes to read
The PDF Document API allows you to reduce PDF file size by optimizing document content. You can compress images and remove unnecessary document elements when saving PDF files.
PDF file size depends on different factors, including embedded images, attachments, metadata, and document structure. Use the appropriate optimization option based on your usage scenario:
| Task | API |
|---|---|
| Reduce the size of embedded images | ImageCompressionOptions |
| Remove unnecessary document elements | OptimizationOptions |
| Compress images and remove unused content | OptimizationOptions, ImageCompressionOptions |
Optimize PDF File Size
Use OptimizationOptions to apply multiple optimization techniques. For example, you can remove unnecessary document elements and compress images in a single operation.
The following code snippet removes metadata, attachments, and thumbnails. It also compresses images to reduce the PDF file size.
package pdf;
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Configure image compression.
ImageCompressionOptions imageOptions = new ImageCompressionOptions();
imageOptions.setCompressionType(ImageCompressionType.JPEG);
imageOptions.setJpegQuality(80);
// Configure optimization options.
OptimizationOptions options = new OptimizationOptions();
options.setRemoveMetadata(true);
options.setRemoveAttachments(true);
options.setRemoveThumbnails(true);
options.setImageCompressionOptions(imageOptions);
// Optimize the PDF document.
pdfDocument.optimize(options);
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("Document_Optimized.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Compress Images
Large images increase PDF file size. Use image compression to reduce the size of embedded images while preserving acceptable image quality.
The PdfDocument.compressImages() method optimizes images in a PDF document. Create an ImageCompressionOptions object and configure image compression settings.
General Compression Settings
| Method | Description |
|---|---|
| setCompressionType() | Specifies the compression algorithm. Use ImageCompressionType.JPEG for lossy compression or ImageCompressionType.FLATE for lossless compression. |
| setJpegQuality() | Specifies JPEG image quality. Valid values range from 0 (lowest quality, smallest file) to 100 (highest quality, largest file). |
| setDownsamplingResolution() | Reduces image resolution before compression. |
| setInterpolationMode() | Specifies the interpolation algorithm used during image resampling. |
| setIgnoreSizeReduction() | Specifies whether to compress images when compression does not reduce their size. |
Compression Settings for Different Image Types
| Method | Description |
|---|---|
| setGrayscaleCompressionType() | Specifies the compression algorithm for grayscale images. |
| setBitonalCompressionType() | Specifies the compression algorithm for bitonal (black-and-white) images. |
| setConvertGrayscale() | Converts color images to grayscale before compression. |
Example: Compress Images
The following code snippet optimizes images with different compression settings:
package pdf;
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Configure image compression options.
ImageCompressionOptions imageCompressionOptions = new ImageCompressionOptions();
// Configure general compression settings.
imageCompressionOptions.setCompressionType(ImageCompressionType.JPEG);
imageCompressionOptions.setJpegQuality(75);
imageCompressionOptions.setDownsamplingResolution(150);
// Convert color images to grayscale.
imageCompressionOptions.setConvertGrayscale(true);
// Configure compression for grayscale images.
imageCompressionOptions.setGrayscaleCompressionType(GrayscaleImageCompressionType.JPEG);
// Configure compression for bitonal images.
imageCompressionOptions.setBitonalCompressionType(BitonalImageCompressionType.CCITT_GROUP_4);
// Compress images.
pdfDocument.compressImages(imageCompressionOptions);
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("Document_Optimized.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}
Remove Unnecessary Content
PDF documents can contain unnecessary elements that increase file size, such as attachments, thumbnails, and metadata. If your application does not require these elements, use the following OptimizationOptions properties to remove them from the PDF document.
| Property | Description |
|---|---|
| setRemoveMetadata() | Removes document metadata, such as the author and creation date. |
| setRemoveAttachments() | Removes embedded files and attachments. |
| setRemoveBookmarkOutlines() | Removes document bookmarks. |
| setRemoveThumbnails() | Removes page thumbnails. |
| setRemoveJavaScriptActions() | Removes JavaScript actions. |
| setRemoveLogicalStructure() | Removes logical structure information used for accessibility. |
Warning
Removed content cannot be restored in the optimized document. Save a copy of the original PDF document if you need to preserve this information.
Example: Remove Metadata, Attachments, and Thumbnails
The following code snippet removes metadata, attachments, and thumbnails from a PDF document:
package pdf;
import com.devexpress.docs.pdf.*;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (FileChannel channel = FileChannel.open(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(channel)) {
// Configure optimization options.
OptimizationOptions options = new OptimizationOptions();
// Remove unnecessary document elements.
options.setRemoveMetadata(true);
options.setRemoveAttachments(true);
options.setRemoveThumbnails(true);
// Optimize the document.
pdfDocument.optimize(options);
try (WritableByteChannel writableByteChannel =
FileChannel.open(Path.of("Document_Optimized.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(writableByteChannel);
}
}
}
}