Optimize PDF Files for Storage - Console C#

This tutorial shows how to quickly and efficiently reduce the size of PDF files while preserving as much of the original file's quality as possible in a C# Windows Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to efficiently reduce the size of PDF files in a Console C# Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019, 2022
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET Framework (Console)

  • Java: Java

Required Knowledge

Before working on the Optimize PDF Files for Storage - Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you don't have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note: How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

Add the PDF Optimizer Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Pdf; 

Add a new method to the Program class named OptimizeFiles(string _pdfFileDir). Call this method inside the Main() method, beneath the call to SetLicense(). The string passed into the OptimizeFiles() method will be the directory pointing to the PDF files you wish to optimize. For the purposes of this tutorial, the following directory will be used: C:\LEADTOOLS23\Resources\Images

Add the below code to the new method to create your PDFOptimizerOptions and optimize the PDFs in the given directory.

C#
static void OptimizeFiles(string _pdfFileDir) 
{ 
    using RasterCodecs _codecs = new RasterCodecs(); 
 
    // Set the path to the PDF utilities dll 
    _codecs.Options.Pdf.InitialPath = @"C:\LEADTOOLS23\Bin\CDLL\x64"; 
 
    string[] _pdfFiles = Directory.GetFiles(_pdfFileDir, "*.pdf"); 
    string _destDir = Path.Combine(_pdfFileDir, "OptimizedPDFs"); 
 
    if (Directory.Exists(_destDir)) 
        Console.WriteLine("Directory exists already."); 
    else 
        Directory.CreateDirectory(_destDir); 
 
    // Create the optimizer options 
    PDFOptimizerOptions myOptimizerOptions = new PDFOptimizerOptions(); 
    myOptimizerOptions.AutoOptimizerMode = PDFAutoOptimizerMode.BestSize; 
 
    myOptimizerOptions.ColorImageDownsamplingMode = PDFDownsamplingMode.Average; 
    myOptimizerOptions.GrayImageDownsamplingMode = PDFDownsamplingMode.Bicubic; 
    myOptimizerOptions.MonoImageDownsamplingMode = PDFDownsamplingMode.Bicubic; 
 
    myOptimizerOptions.ColorImageDownsampleFactor = 2.0; 
    myOptimizerOptions.GrayImageDownsampleFactor = 2.0; 
    myOptimizerOptions.MonoImageDownsampleFactor = 2.0; 
 
    myOptimizerOptions.ColorImageDPI = 150; 
    myOptimizerOptions.GrayImageDPI = 150; 
    myOptimizerOptions.MonoImageDPI = 150; 
 
    myOptimizerOptions.ColorImageCompression = RasterImageFormat.Jpeg; 
    myOptimizerOptions.GrayImageCompression = RasterImageFormat.RasPdfLzw; 
    myOptimizerOptions.MonoImageCompression = RasterImageFormat.Jbig; 
 
    myOptimizerOptions.EmbedAllFonts = false; 
    myOptimizerOptions.SubsetFonts = true; 
 
    foreach (string _pdfFile in _pdfFiles) 
    { 
        string _pdfFileName = Path.GetFileNameWithoutExtension(_pdfFile); 
        string _destFile = Path.Combine(_destDir, $"{_pdfFileName}_OPTIMIZED.pdf"); 
 
        CodecsImageInfo _info = _codecs.GetInformation(_pdfFile, true); 
        long _beforeBytes = _info.SizeDisk; 
        Console.WriteLine($"Size of {_pdfFileName}.pdf: {_beforeBytes}"); 
 
        PDFFile _pdf = new PDFFile(_pdfFile); 
        _pdf.OptimizerOptions = myOptimizerOptions; 
        _pdf.Optimize(_destFile); 
        Console.WriteLine($"PDF optimized and saved to {_destFile}"); 
 
        CodecsImageInfo _afterInfo = _codecs.GetInformation(_destFile, true); 
        long _afterBytes = _afterInfo.SizeDisk; 
        Console.WriteLine($"Size of optimized {_pdfFileName}.pdf: {_afterBytes}\n"); 
    } 
 
} 

Note: The AutoOptimizerMode property sets the enumeration that indicates how you want to optimize your PDF files (BestQuality, BestSize, AverageQuality, etc.).

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application grabs all the PDF files in the given directory, optimizes the files based on the PDFOptimizerOptions, and exports the PDF to the created directory.

Screenshot showing the optimize results.

Wrap-up

This tutorial showed how to add the necessary references to optimize each PDF file in a given directory for storage. Also, we covered how to work with the PDFFile and PDFOptimizerOptions classes.

See Also

Help Version 23.0.2024.4.23
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.