Optimize PDF Documents
- 5 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 several factors, including embedded images, attachments, metadata, and document structure. Use the appropriate optimization method based on your usage scenario:
| Task | API |
|---|---|
| Reduce the size of embedded images | CompressImages(ImageCompressionOptions) |
| Remove unnecessary document elements | Optimize(OptimizationOptions) |
| Compress images and remove unused content | Optimize(OptimizationOptions) with specified ImageCompressionOptions |
Compress Images
Large images increase PDF file size. Use image compression to reduce the size of embedded images while maintaining acceptable image quality.
The PdfDocument.CompressImages(ImageCompressionOptions) method optimizes images in a PDF document. Create an ImageCompressionOptions object and configure image compression settings.
General Compression Settings
| Method | Description |
|---|---|
| CompressionType | Specifies the compression algorithm. Use ImageCompressionType.Jpeg for lossy compression or ImageCompressionType.Flate for lossless compression. |
| JpegQuality | Specifies JPEG image quality. Valid values range from 0 (lowest quality, smallest file) to 100 (highest quality, largest file). |
| DownsamplingResolution | Reduces image resolution before compression. |
| InterpolationMode | Specifies the interpolation algorithm used during image resampling. |
| IgnoreSizeReduction | Specifies whether to compress images when compression does not reduce their size. |
Compression Settings for Different Image Types
| Method | Description |
|---|---|
| GrayscaleCompressionType | Specifies the compression algorithm for grayscale images. |
| BitonalCompressionType | Specifies the compression algorithm for bitonal (black-and-white) images. |
| ConvertGrayscale | Converts color images to grayscale before compression. |
Example: Compress Images in a PDF Document
The following code snippet applies JPEG compression to images in a PDF document, converts color images to grayscale, and specifies compression algorithms for grayscale and bitonal images:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Configure image compression options.
var options = new ImageCompressionOptions()
{
CompressionType = ImageCompressionType.Jpeg,
JpegQuality = 75,
DownsamplingResolution = 150,
ConvertGrayscale = true,
GrayscaleCompressionType = GrayscaleImageCompressionType.Jpeg,
BitonalCompressionType = BitonalImageCompressionType.CcittGroup4
};
// Optimize the document.
pdfDocument.CompressImages(options);
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document_Optimized.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
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. Pass the configured OptimizationOptions object to the PdfDocument.Optimize(OptimizationOptions) method.
Use the PdfDocument.Optimize(OptimizationOptions) method to apply multiple optimization techniques. For example, you can remove unnecessary document elements and compress images in a single operation.
| Property | Description |
|---|---|
| RemoveMetadata | Removes document metadata, such as the author and creation date. |
| RemoveAttachments | Removes embedded files and attachments. |
| RemoveBookmarkOutlines | Removes document bookmarks. |
| RemoveThumbnails | Removes page thumbnails. |
| RemoveJavaScriptActions | Removes JavaScript actions. |
| RemoveLogicalStructure | Removes logical structure information used for accessibility. |
Warning
Removed content cannot be restored in the optimized document. Keep a copy of the original PDF document if you need to preserve this information.
Example: Remove Unnecessary Content from a PDF Document
The following code snippet removes metadata, attachments, and thumbnails from a PDF document:
using System.IO;
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead("Document.pdf")))
{
// Remove unnecessary elements
var options = new OptimizationOptions()
{
RemoveMetadata = true,
RemoveAttachments = true,
RemoveThumbnails = true
};
// Optimize the document.
pdfDocument.Optimize(options);
// Save the optimized document
Directory.CreateDirectory("output");
using (FileStream outputFileStream = File.Create(Path.Combine("output", "Document_Optimized.pdf")))
{
pdfDocument.Save(outputFileStream);
}
}
Apply Multiple Optimization Techniques
Use the PdfDocument.Optimize(OptimizationOptions) method to apply multiple optimization techniques. For example, you can remove unnecessary document elements and compress images in a single operation. Specify the ImageCompressionOptions property of the OptimizationOptions object to configure image compression settings.
The following code example removes metadata, attachments, and thumbnails from a PDF document and compresses images using JPEG compression:
using DevExpress.Docs.Pdf;
using (PdfDocument pdfDocument =
new PdfDocument(File.OpenRead(@"Document.pdf")))
{
var options = new OptimizationOptions()
{
// Remove unnecessary elements
RemoveMetadata = true,
RemoveAttachments = true,
RemoveThumbnails = true,
// Configure image compression settings
ImageCompressionOptions = new ImageCompressionOptions()
{
CompressionType = ImageCompressionType.Jpeg,
JpegQuality = 75,
DownsamplingResolution = 150,
ConvertGrayscale = true,
GrayscaleCompressionType = GrayscaleImageCompressionType.Jpeg,
BitonalCompressionType = BitonalImageCompressionType.CcittGroup4
}
};
// Optimize the document
pdfDocument.Optimize(options);
// Save the optimized document
using (FileStream outputFileStream = File.Create(@"Document_Optimized.pdf"))
{
pdfDocument.Save(outputFileStream);
}
}