Compress PDF document using save options

Docotic.Pdf Library Help > Samples > Compression > Compress PDF document using save options
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 but may also slow down saving of the document. This is especially true for large documents.

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

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CompressWithSaveOptions
    {
        public static void Main()
        {
            const string originalFile = @"Sample Data\BRAILLE CODES WITH TRANSLATION.pdf";
            const string compressedFile = "CompressWithSaveOptions.pdf";

            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // 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;

                pdf.Save(compressedFile);
            }

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

            Process.Start(compressedFile);
        }
    }
}
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 CompressWithSaveOptions
        Public Shared Sub Main()
            Const originalFile As String = "Sample Data\BRAILLE CODES WITH TRANSLATION.pdf"
            Const compressedFile As String = "CompressWithSaveOptions.pdf"

            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' 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

                pdf.Save(compressedFile)
            End Using

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

            Process.Start(compressedFile)
        End Sub
    End Class
End Namespace