Skip to main content
Version: 1.0

Optimize a PDF document

The Pdf Tools SDK lets you 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.

The Pdf Tools SDK currently supports four 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).

Steps to optimize a document:

  1. Create an Optimizer object.
  2. Set an optimization profile.
  3. Run the OptimizeDocument method.

Before you begin

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

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);