Skip to main content
Version: Version 1.7

Compress and optimize a PDF document

The Pdftools SDK lets you compress and optimize a PDF document for a specific purpose. Depending on the specified purpose, different operations are performed on the PDF document, such as reducing image resolution or removing unnecessary objects.
You can encrypt the output PDF as part of the optimization process.

Quick start

Download the full sample now in C#, Java, C, and Python.

The Pdftools SDK currently supports five optimization profiles that are intended for the following purposes:

  • Archive: Prepare a document for archiving in PDF/A format, reducing the file size prior to converting to PDF/A.
  • Minimal file size: Remove redundant data and reduce image resolution to achieve a minimal viable file size.
  • Print: Compress the file, but ensure that the print quality of the document is not affected.
  • Web: Similar to the Print profile, but ensure that the viewing quality of the document is not affected when viewing on digital devices (e.g., in a web browser).
  • Mixed raster content: Apply Mixed Raster Content (MRC) optimization intended for compressing scanned documents that contain large amounts of text.

Steps to optimize a document:

  1. Reading the input document
  2. Creating an Optimizer object
  3. Setting an optimization profile
  4. Running the OptimizeDocument method
  5. Full example

Before you begin

Reading the input document

First you need to read the PDF document you want to validate. To do this, load the input document from the file system into a read-only input Document. This Document can then later be passed to the Converter.

// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);

Creating an Optimizer object

You start the document optimization process by creating an Optimizer object. The Optimizer object takes an input PDF document and optimizes the content for a specific purpose. It then outputs the result to a document object that can be persisted to a file or used as an input for further processing.

// Create the Optimizer object.

Optimizer optimizer = new Optimizer();

Setting an optimization profile

The Profile object controls the behavior of the Optimizer object during the optimization process. The profile can be set to one of four subclasses that are used for different purposes: Archive, MinimalFileSize, Print, and Web.

This example uses the Web profile.

// Create a profile that controls the Optimizer output behavior.
// The Web profile is used to optimize documents for electronic document exchange.

var profile = new Profiles.Web(); // Archive, MinimalFileSize, Print, Web

// Optionally, the profile's parameters can be changed according to the
// requirements of your optimization process

Running the OptimizeDocument method

After creating the Optimize and Profile objects, the final step is to call the OptimizeDocument method. The document produced by this method has the optimizations applied to it that are defined by the optimization profile.

If the output document cannot be created, a detailed error message is generated.

// Create the output stream.
using var outStr = File.Create(outPath);

// Optimize the document and save it to a file.
using var outDoc = optimizer.OptimizeDocument(inDoc, outStr, profile);
tip

During the optimization process, the output file may be encrypted and may be assigned permissions. For more information on how PDF documents are encrypted, see Encryption in PDF documents.

Full example

// Open the PDF document to validate
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);

// Create the Optimizer object.
Optimizer optimizer = new Optimizer();

// Create a profile that controls the Optimizer output behavior.
// The Web profile is used to optimize documents for electronic document exchange.
var profile = new Profiles.Web();

// Create the output stream.
using var outStr = File.Create(outPath);

// Optimize the document and save it to a file.
using var outDoc = optimizer.OptimizeDocument(inDoc, outStr, profile);