Document Optimization

In DynamicPDF Core Suite there are several ways to optimize a document; combining duplicate images, stripping out unneeded content, setting a high compression level or setting a document to be linearized (optimized for fast web viewing only). These items are discussed below.

Combining Images

One way to achieve an optimized PDF is to tell DynamicPDF Core Suite to combine the image data of any images that are shared throughout one or more PDF documents. In other words, if there was an Image_A inside PDF_A and the identical Image_A was inside PDF_B, then when you merge PDF_A and PDF_B merger can combine the image data content into one therefore reducing the overall file size of the output PDF. Setting a high compression level will also further increase the file size reduction potential.

The code below demonstrates how to combine a PDF's resources (Images) along with setting a high CompressionLevel.

DocumentOptimization optimization = new DocumentOptimization (true);
Document.Optimization = optimization;
Document.Compressionlevel = 9;        
Dim optimization As DocumentOptimization = New DocumentOptimization(True)
Document.Optimization = optimization
Document.Compressionlevel = 9  

Stripping Unneeded Content

Another way to potentially further reduce the file size would be to remove any content of the PDF document that is being merged in that is not needed. A PDF content may contain JavaScript, Bookmarks, Meta Data, Logical Structure, Embedded Files, Form Fields, etc. all of which may not be a required element in the final output PDF. Therefore, removing them from the document as they are merged in has the potential of reducing the file size. All this is achieved by using the MergeOptions class which allows users to set exactly what is and is not merged into the file being created.

The code below demonstrates how to exclude JavaScript, bookmarks, Metadata, logical structure, embedded files from the document.

MergeOptions mergeOptions = new MergeOptions();
mergeOptions.LogicalStructure = false;
mergeOptions.Outlines = false;
mergeOptions.DocumentJavaScript = false;
mergeOptions.EmbeddedFiles = false;
MergeDocument document = new MergeDocument(pdfFilePath, mergeOptions);
document.Draw(pdfFilePath);        
Dim MyMergeOptions As MergeOptions = New MergeOptions()
MyMergeOptions.LogicalStructure = False
MyMergeOptions.Outlines = False
MyMergeOptions.DocumentJavaScript = False
MyMergeOptions.EmbeddedFiles = False
Dim Mydocument As MergeDocument = New MergeDocument(pdfFilePath, MyMergeOptions)
Mydocument.Draw(pdfFilePath)              

Linearizing the PDF (Fast Web View)

Setting the PDF file to be linearized will not optimize it in terms of file size (in fact, a linearized PDF will be just slightly larger than its non-linearized counterpart) but it allows the PDF, once on the server, to serve up pages to the browser as they are become available (as opposed to waiting for the entire file to be downloaded).

We can also optimize the PDF for fast web view using the below code.

MergeDocument.DefaultPdfFormat = PdfFormat.Linearized;        
MergeDocument.DefaultPdfFormat = PdfFormat.Linearized

In this topic