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.
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:
- Reading the input document
- Creating an Optimizer object
- Setting an optimization profile
- Running the OptimizeDocument method
- Full example
You need to initialize the library.
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
.
- .NET
- Java
// Open image document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
com.pdftools.pdf.Document inDoc = com.pdftools.pdf.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.
- .NET
- Java
// Create the Optimizer object.
Optimizer optimizer = new Optimizer();
// 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.
- .NET
- Java
// 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
// Create the profile that defines the optimization parameters.
// The Web profile is used to optimize documents for electronic document exchange.
Web profile = new 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.
- .NET
- Java
// 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);
// Create output stream
FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Optimize the document
Document outDoc = optimizer.optimizeDocument(inDoc, outStr, profile));
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
- .NET
- Java
// 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);
// Create the Optimizer object.
Optimizer optimizer = new Optimizer();
// Create the profile that defines the optimization parameters.
// The Web profile is used to optimize documents for electronic document exchange.
Web profile = new Web();
// Optionally, the profile's parameters can be changed according to the
// requirements of your optimization process.
try (
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
Document inDoc = Document.open(inStr);
// Create output stream
FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Optimize the document
Document outDoc = optimizer.optimizeDocument(inDoc, outStr, profile))
{
}