Optimize PDF documents in C++

Optimize PDF resources, compress images, remove unused data, flatten interactive content, and convert pages to grayscale with Aspose.PDF for C++.

Report input and output file sizes

Several examples use the following helper to compare file sizes. Save it as optimize-file-sizes.h next to the source files that include it. Optimization results depend on the PDF; an output file can be the same size or larger.

  1. Read the input file size before modifying the PDF.
  2. Read the saved output file size.
  3. Print both sizes for comparison.

#include <cstdint>
#include <system/console.h>
#include <system/io/file_info.h>
#include <system/string.h>

inline void PrintOptimizationFileSizes(int64_t originalSize, System::String outputFile)
{

    auto output = System::MakeObject<System::IO::FileInfo>(outputFile);
    System::Console::WriteLine(
        u"Original file size: {0}. Reduced file size: {1}",
        originalSize, output->get_Length());
}

Optimize for web viewing

Web optimization is distinct from reducing resources and does not guarantee a smaller file.

  1. Open the input PDF as a Document.
  2. Call Optimize() to organize the PDF for web access.
  3. Save the result with Save().
  4. Print the input and output sizes using PrintOptimizationFileSizes().

Source example: optimize-document-01.cpp.

#include <Aspose.PDF.Cpp/Document.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void OptimizePdf(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    document->Optimize();
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Optimize resources with default settings

  1. Open the input PDF as a Document.
  2. Call OptimizeResources() using its default settings.
  3. Save the result with Save().
  4. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void ReduceSizePdf(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    document->OptimizeResources();
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Compress images

Image compression can reduce visual quality. Review the output before choosing a quality setting.

  1. Open the input PDF as a Document.
  2. Create OptimizationOptions.
  3. Enable CompressImages and set ImageQuality to 50 through ImageCompressionOptions.
  4. Call OptimizeResources() with the options.
  5. Save the result with Save().
  6. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include <Aspose.PDF.Cpp/Optimization/ImageCompressionOptions.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void ShrinkingOrCompressingAllImages(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->get_ImageCompressionOptions()->set_CompressImages(true);
    options->get_ImageCompressionOptions()->set_ImageQuality(50);
    document->OptimizeResources(options);
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Remove unused objects

  1. Open the input PDF as a Document.
  2. Create OptimizationOptions and enable RemoveUnusedObjects.
  3. Call OptimizeResources() with the options.
  4. Save the result with Save().
  5. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void RemovingUnusedObjects(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->set_RemoveUnusedObjects(true);
    document->OptimizeResources(options);
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Remove unused streams

  1. Open the input PDF as a Document.
  2. Create OptimizationOptions and enable RemoveUnusedStreams.
  3. Call OptimizeResources() with the options.
  4. Save the result with Save().
  5. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void RemovingUnusedStreams(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->set_RemoveUnusedStreams(true);
    document->OptimizeResources(options);
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}
  1. Open the input PDF as a Document.
  2. Create OptimizationOptions and enable LinkDuplicateStreams.
  3. Call OptimizeResources() with the options.
  4. Save the result with Save().
  5. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void LinkingDuplicateStreams(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->set_LinkDuplicateStreams(true);
    document->OptimizeResources(options);
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Unembed fonts

Removing embedded fonts makes rendering dependent on fonts available to the viewer and can affect appearance.

  1. Open the input PDF as a Document.
  2. Create OptimizationOptions and enable UnembedFonts.
  3. Call OptimizeResources() with the options.
  4. Save the result with Save().
  5. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void UnembedFonts(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->set_UnembedFonts(true);
    document->OptimizeResources(options);
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Flatten annotations

Reverse iteration accounts for annotations being removed during flattening.

  1. Open the input PDF as a Document.
  2. Iterate through every Page.
  3. Traverse the annotations in reverse order and call Flatten() on each one.
  4. Save the result with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Annotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void FlattenAnnotations(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (auto page : document->get_Pages()) {
        auto annotations = page->get_Annotations();
        // Flatten removes annotations, so visit the collection in reverse.
        for (int index = annotations->get_Count(); index >= 1; --index) {
            annotations->idx_get(index)->Flatten();
        }
    }
    document->Save(outputFile);
}

Flatten form fields

Flattened fields no longer provide interactive form entry.

  1. Open the input PDF as a Document.
  2. Get the document form and take a snapshot of its fields.
  3. Call Flatten() on each field in the snapshot.
  4. Save the result with Save().
  5. Print the input and output sizes.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Form.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Forms/Field.h>
#include <system/array.h>
#include "optimize-file-sizes.h"

using namespace System;
using namespace Aspose::Pdf;

void FlattenForms(String inputFile, String outputFile)
{
    const auto originalSize = System::MakeObject<System::IO::FileInfo>(inputFile)->get_Length();
    auto document = MakeObject<Document>(inputFile);
    auto form = document->get_Form();
    if (form != nullptr) {
        // Snapshot the fields before Flatten changes the form collection.
        auto fields = form->get_Fields();
        for (auto& field : fields) {
            field->Flatten();
        }
    }
    document->Save(outputFile);
    PrintOptimizationFileSizes(originalSize, outputFile);
}

Convert pages to grayscale

Grayscale conversion changes page colors; file-size reduction depends on the input.

  1. Open the input PDF as a Document.
  2. Iterate through each Page and call MakeGrayscale().
  3. Save the result with Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;

void ConvertPdfFromRgbColorspaceToGrayscale(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (auto& page : document->get_Pages()) {
        page->MakeGrayscale();
    }
    document->Save(outputFile);
}

Use Flate image encoding

  1. Open the input PDF as a Document.
  2. Create OptimizationOptions.
  3. Set the ImageCompressionOptions Encoding property to ImageEncoding::Flate.
  4. Call OptimizeResources() with the options.
  5. Save the result with Save()
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Optimization/OptimizationOptions.h>
#include <Aspose.PDF.Cpp/Optimization/ImageCompressionOptions.h>
#include <Aspose.PDF.Cpp/Optimization/ImageEncoding.h>

using namespace System;
using namespace Aspose::Pdf;

void UsingFlatedecodeCompression(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto options = MakeObject<Optimization::OptimizationOptions>();
    options->get_ImageCompressionOptions()->set_Encoding(Optimization::ImageEncoding::Flate);
    document->OptimizeResources(options);
    document->Save(outputFile);
}