Compress existing PDF document

Docotic.Pdf Library Help > Samples > General operations > Compress existing PDF document
Docotic.Pdf documentation

This sample shows how to optimize size of an existing PDF document using PdfDocument.SaveOptions property.

PDF file is internally a dump of PDF objects. Save options can be used to specify which optimizations should be applied to PDF objects while saving a PDF file. All these optimizations don't affect contents (text, images, bookmarks and anything else) of the PDF file. These optimizations only affect how PDF objects are written and compressed in an output PDF file.

Turning on all save options shown in this sample may help to achieve great compression ratio of the output PDF file. This is especially true for large documents.

Also turning on all save options shown in this sample may slow down saving of the document.

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace BitMiracle.Docotic.Pdf.Samples
    public static class CompressExistingDocument
        public static void Main()
            const string originalFile = @"Sample Data\form.pdf";
            const string compressedFile = "CompressExistingDocument.pdf";

            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit
            // for more information.

            using (PdfDocument pdf = new PdfDocument(originalFile))
                pdf.SaveOptions.Compression = PdfCompression.Flate;
                pdf.SaveOptions.UseObjectStreams = true;
                pdf.SaveOptions.RemoveUnusedObjects = true;
                pdf.SaveOptions.OptimizeIndirectObjects = true;
                pdf.SaveOptions.WriteWithoutFormatting = true;


            FileInfo originalFileInfo = new FileInfo(originalFile);
            FileInfo compressedFileInfo = new FileInfo(compressedFile);
            MessageBox.Show(String.Format("Original file size: {0} bytes;\r\nCompressed file size: {1} bytes", originalFileInfo.Length, compressedFileInfo.Length));

Visual Basic 
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms

Imports Microsoft.VisualBasic

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CompressExistingDocument
        Public Shared Sub Main()
            Const originalFile As String = "Sample Data\form.pdf"
            Const compressedFile As String = "CompressExistingDocument.pdf"

            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit
            ' for more information.

            Using pdf As New PdfDocument(originalFile)
                pdf.SaveOptions.Compression = PdfCompression.Flate
                pdf.SaveOptions.UseObjectStreams = True
                pdf.SaveOptions.RemoveUnusedObjects = True
                pdf.SaveOptions.OptimizeIndirectObjects = True
                pdf.SaveOptions.WriteWithoutFormatting = True

            End Using

            Dim originalFileInfo As New FileInfo(originalFile)
            Dim compressedFileInfo As New FileInfo(compressedFile)
            MessageBox.Show([String].Format("Original file size: {0} bytes;" & vbCr & vbLf & "Compressed file size: {1} bytes", originalFileInfo.Length, compressedFileInfo.Length))

        End Sub
    End Class
End Namespace