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.
- Read the input file size before modifying the PDF.
- Read the saved output file size.
- 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.
- Open the input PDF as a Document.
- Call Optimize() to organize the PDF for web access.
- Save the result with Save().
- 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
- Open the input PDF as a Document.
- Call OptimizeResources() using its default settings.
- Save the result with Save().
- 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.
- Open the input PDF as a Document.
- Create OptimizationOptions.
- Enable CompressImages and set ImageQuality to 50 through ImageCompressionOptions.
- Call OptimizeResources() with the options.
- Save the result with Save().
- 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
- Open the input PDF as a Document.
- Create OptimizationOptions and enable RemoveUnusedObjects.
- Call OptimizeResources() with the options.
- Save the result with Save().
- 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
- Open the input PDF as a Document.
- Create OptimizationOptions and enable RemoveUnusedStreams.
- Call OptimizeResources() with the options.
- Save the result with Save().
- 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);
}
Link duplicate streams
- Open the input PDF as a Document.
- Create OptimizationOptions and enable LinkDuplicateStreams.
- Call OptimizeResources() with the options.
- Save the result with Save().
- 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.
- Open the input PDF as a Document.
- Create OptimizationOptions and enable UnembedFonts.
- Call OptimizeResources() with the options.
- Save the result with Save().
- 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.
- Open the input PDF as a Document.
- Iterate through every Page.
- Traverse the annotations in reverse order and call Flatten() on each one.
- 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.
- Open the input PDF as a Document.
- Get the document form and take a snapshot of its fields.
- Call Flatten() on each field in the snapshot.
- Save the result with Save().
- 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.
- Open the input PDF as a Document.
- Iterate through each Page and call MakeGrayscale().
- 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
- Open the input PDF as a Document.
- Create OptimizationOptions.
- Set the ImageCompressionOptions Encoding property to ImageEncoding::Flate.
- Call OptimizeResources() with the options.
- 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);
}