PDF: Optimize and compress PDF document
In This Topic
Under optimizing and compressing of PDF document is mentioned some actions, which lead to reducing of size of PDF document.
To optimize a PDF document the SDK can perform the following actions:
- remove unused objects from PDF document
- remove broken bookmarks and links
- remove embedded thumbnails of PDF pages
- remove annotations
- remove bookmarks
- remove the document metadata
- remove the embedded files from PDF document
- compress data
- re-compress data with more optimal compression algorithm
- divide image resources to layers using MRC compression algorithm
- reduce the resolution of image resources
- reduce the color depth of image resources
SDK has many classes and methods, which allow to reduce the PDF document size.
The
PdfDocumentCompressorCommand class is the PDF processing command that simplifies the PDF document compression process
and contains all settings in one class.
The SDK provides WinForms PDF Compressor Demo and PDF Compressor Console Demo applications, which demonstrate how to reach the maximum compression for any PDF document.
Source codes of the demo applications are available in C# and VB.NET.
Usage of MRC algorithm for optimizing and compressing PDF documents
To optimize PDF documents acquired from scanner very often is used MRC technology, which allows to divide the image acquired from
scanner into layers and compress it the more optimal way. Detailed information about MRC compression algorithm,
which is implemented in the SDK, can be found
here.
Optimization of images and data of PDF document
PdfDocument.Optimize method is intended for optimizing images and data of PDF document.
Here is an example that demonstrates how to change version of PDF document (it'll be created a document of version 1.6 with compressed xref table) and compress all color image resources of document using JPEG compression algorithm:
public static void PackPdf(string pdfFileName, bool optimize)
{
// open PDF document to Read/Write
Vintasoft.Imaging.Pdf.PdfDocument pdfDocument =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName);
// create PdfFormat - 1.6 format, with compressed cross-reference table
Vintasoft.Imaging.Pdf.PdfFormat format =
new Vintasoft.Imaging.Pdf.PdfFormat("1.6", true, true);
if (optimize)
{
// set JPEG Quality to 80
Vintasoft.Imaging.Pdf.PdfCompressionSettings.DefaultSettings.JpegQuality = 80;
// create optimize settings
Vintasoft.Imaging.Pdf.PdfOptimizeSettings optimizeSettings =
new Vintasoft.Imaging.Pdf.PdfOptimizeSettings(
Vintasoft.Imaging.Pdf.PdfCompression.Jpeg, //compression for Color images - JPEG
Vintasoft.Imaging.Pdf.PdfCompression.Undefined, //compression for B/W images - not changed
Vintasoft.Imaging.Pdf.PdfCompression.Undefined); //compression for Data - not changed
// optimize and Pack document
pdfDocument.Optimize(format, optimizeSettings);
}
else
{
// pack document
pdfDocument.Pack(format);
}
// free resources
pdfDocument.Dispose();
}
Public Shared Sub PackPdf(pdfFileName As String, optimize As Boolean)
' open PDF document to Read/Write
Dim pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName)
' create PdfFormat - 1.6 format, with compressed cross-reference table
Dim format As New Vintasoft.Imaging.Pdf.PdfFormat("1.6", True, True)
If optimize Then
' set JPEG Quality to 80
Vintasoft.Imaging.Pdf.PdfCompressionSettings.DefaultSettings.JpegQuality = 80
' create optimize settings
'compression for Color images - JPEG
'compression for B/W images - not changed
Dim optimizeSettings As New Vintasoft.Imaging.Pdf.PdfOptimizeSettings(Vintasoft.Imaging.Pdf.PdfCompression.Jpeg, Vintasoft.Imaging.Pdf.PdfCompression.Undefined, Vintasoft.Imaging.Pdf.PdfCompression.Undefined)
'compression for Data - not changed
' optimize and Pack document
pdfDocument.Optimize(format, optimizeSettings)
Else
' pack document
pdfDocument.Pack(format)
End If
' free resources
pdfDocument.Dispose()
End Sub