officefileapi-405623-pdf-document-api-document-manipulation-optimize-documents.md
The PDF Document API allows you to optimize PDF documents to reduce file size. You can compress images with different quality settings and resolutions, and remove unnecessary elements such as metadata, attachments, bookmarks, thumbnails, JavaScript actions, and logical structure information.
This topic contains API reference documentation and C#/VB.NET code examples that perform the following actions:
Use the OptimizeDocument(PdfImageCompressionOptions) method to reduce file size by compressing images. Create a PdfImageCompressionOptions object and configure the following settings:
The following code snippet compresses images in a PDF document with specialized compression algorithms for different image types:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load the document
processor.LoadDocument("Document.pdf");
// Configure image compression options
var options = new PdfImageCompressionOptions() {
// General compression settings
CompressionType = PdfImageCompressionType.Jpeg,
JpegQuality = 75,
DownsamplingResolution = 150,
// Convert color images to grayscale
ConvertGrayscale = true,
// Compression for grayscale images
GrayscaleCompressionType = PdfGrayscaleImageCompressionType.Jpeg,
// Compression for bitonal (black and white) images
BitonalCompressionType = PdfBitonalImageCompressionType.CCITTGroup4
};
// Optimize the document
processor.OptimizeDocument(options);
// Save the result
processor.SaveDocument("Document.optimized.pdf");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
' Load the document
processor.LoadDocument("Document.pdf")
' Configure image compression options
Dim options As New PdfImageCompressionOptions() With {
.CompressionType = PdfImageCompressionType.Jpeg,
.JpegQuality = 75,
.DownsamplingResolution = 150,
.ConvertGrayscale = True,
.GrayscaleCompressionType = PdfGrayscaleImageCompressionType.Jpeg,
.BitonalCompressionType = PdfBitonalImageCompressionType.CCITTGroup4
}
' Optimize the document
processor.OptimizeDocument(options)
' Save the result
processor.SaveDocument("Document.optimized.pdf")
End Using
Use the OptimizeDocument(PdfOptimizationOptions) method to optimize file size by removing unused elements. Create a PdfOptimizationOptions object and specify elements to remove:
You can also compress images as part of comprehensive optimization. To do this, assign a PdfImageCompressionOptions object to the ImageCompressionOptions property.
The following code snippet removes metadata, attachments, and compresses images:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load the document
processor.LoadDocument("Document.pdf");
// Configure optimization options
var options = new PdfOptimizationOptions() {
// Remove unnecessary elements
RemoveMetadata = true,
RemoveAttachments = true,
RemoveThumbnails = true,
// Compress images
ImageCompressionOptions = new PdfImageCompressionOptions() {
CompressionType = PdfImageCompressionType.Jpeg,
JpegQuality = 80,
}
};
// Optimize the document
processor.OptimizeDocument(options);
// Save the result
processor.SaveDocument("Document.optimized.pdf");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
' Load the document
processor.LoadDocument("Document.pdf")
' Configure optimization options
Dim options As New PdfOptimizationOptions() With {
' Remove unnecessary elements
.RemoveMetadata = True,
.RemoveAttachments = True,
.RemoveThumbnails = True,
' Compress images
.ImageCompressionOptions = New PdfImageCompressionOptions() With {
.CompressionType = PdfImageCompressionType.Jpeg,
.JpegQuality = 80
}
}
' Optimize the document
processor.OptimizeDocument(options)
' Save the result
processor.SaveDocument("Document.optimized.pdf")
End Using
Activate the UseObjectStreams option to use object streams when saving PDF documents. Object streams (PDF 1.5+) pack multiple PDF objects into a single compressed stream to reduce file size.
Set the UseObjectStreams property to true in a PdfSaveOptions object and pass it to the SaveDocument method.
The following code snippet merges multiple PDF files into a single document and uses object streams to reduce the output file size:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Create an empty document with object streams enabled
var saveOptions = new PdfSaveOptions() {
UseObjectStreams = true
};
processor.CreateEmptyDocument("Merged.pdf", saveOptions);
// Append multiple PDF files
processor.AppendDocument("Document1.pdf");
processor.AppendDocument("Document2.pdf");
processor.AppendDocument("Document3.pdf");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
' Create an empty document with object streams enabled
Dim saveOptions As New PdfSaveOptions() With {
.UseObjectStreams = True
}
processor.CreateEmptyDocument("Merged.pdf", saveOptions)
' Append multiple PDF files
processor.AppendDocument("Document1.pdf")
processor.AppendDocument("Document2.pdf")
processor.AppendDocument("Document3.pdf")
End Using