Optimize and compress files in C++

High-performance C++ library to optimize file's content and size retaining the original quality and baseline data

Use C++ to obtain maximum document processing speed. This is a powerful solution to compress Word documents in C++. Try it online for free!

View code snippet

Compress Word documents in C++

Make large files smaller without loss of quality. Delete unnecessary and unused data. Shrink the size of Word and HTML files in C++. Our C++ library provides developers with an integrated API to optimize documents in various formats.

This C++ library is a stand-alone solution and doesn't require any third party software installed.

Reduce the size of Word and HTML files using C++

The maximum effect can be obtained by optimizing high-resolution images, as well as documents containing such images. Our C++ library will help you to compress DOCX, DOC, RTF, ODT, EPUB, HTML, JPG, PNG files.

Optimization of documents without loss of quality made easy with 'Aspose.Words for C++'. The following example shows how to shrink the size of a Word document in C++:

C++ code example to optimize file's content and size
Upload a file you want to optimize/compress
Run code
Select the target format from the list
using namespace Aspose::Words;

auto doc = MakeObject<Document>(u"Input.docx");
doc->Cleanup();

auto nodes = doc->GetChildNodes(NodeType::Shape, true);
for (const auto& shape : System::IterateOver<Shape>(nodes))
{
    if (shape->get_HasImage())
    {
        // It's up to developer to choose the library for image compression.
        auto image = 
           System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream());

        // ...
        // Compress image and set it back to the shape.
        shape->get_ImageData()->SetImage("yourCompressedImage");
    }
}

auto saveOptions = MakeObject<OoxmlSaveOptions>();
saveOptions->set_CompressionLevel(CompressionLevel::Maximum);

doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // It's up to developer to choose the library for image compression. auto image = System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream()); // ... // Compress image and set it back to the shape. shape->get_ImageData()->SetImage("yourCompressedImage"); } } auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->set_CacheBackgroundGraphics(true); doc->Save(u"Output.docx", saveOptions); using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // It's up to developer to choose the library for image compression. auto image = System::Drawing::Image::Fromstream(shape->get_ImageData()->ToStream()); // ... // Compress image and set it back to the shape. shape->get_ImageData()->SetImage("yourCompressedImage"); } } doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(u"Input.docx"); doc->Cleanup(); auto nodes = doc->GetChildNodes(NodeType::Shape, true); for (const auto& shape : System::IterateOver<Shape>(nodes)) { if (shape->get_HasImage()) { // It's up to developer to choose the library for image compression. auto image = System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream()); // ... // Compress image and set it back to the shape. shape->get_ImageData()->SetImage("yourCompressedImage"); } } auto saveOptions = MakeObject<OoxmlSaveOptions>(); saveOptions->set_CompressionLevel(CompressionLevel::Maximum); doc->Save(u"Output.docx", saveOptions); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto shape = builder->InsertImage(u"Input.docx"); // Update page settings to crop the document to fit the size of the picture. auto pageSetup = builder->get_PageSetup(); pageSetup->set_PageWidth(shape->get_Width()); pageSetup->set_PageHeight(shape->get_Height()); pageSetup->set_TopMargin(0); pageSetup->set_LeftMargin(0); pageSetup->set_BottomMargin(0); pageSetup->set_RightMargin(0); auto saveOptions = MakeObject<ImageSaveOptions>(SaveFormat::Docx); doc->Save(u"Output.docx", saveOptions);
Run code

How to compress a file C++: Word, PDF, GIF, ZIP

  1. Install Aspose.Words for C++.
  2. Add a library reference (import the library) to your C++ project.
  3. Open the source file in C++.
  4. Compress documents and images without losing quality.
  5. Call the Document.Cleanup() method. The resulting code may vary depending on the input/output file formats.
  6. Get the result as a compressed file.

C++ library to optimize and compress files

There are three options to install Aspose.Words for C++ to your developer environment. Please choose one that resembles your needs and follow the step-by-step instructions:

System Requirements

You can use this C++ library to develop software on Microsoft Windows, Linux and macOS operating systems:

  • GCC >= 6.3.0 and Clang >= 3.9.1 are required for Linux
  • Xcode >= 12.5.1, Clang and libc++ are required for macOS

If you develop software for Linux or macOS, please check information on additional library dependencies (fontconfig and mesa-glu open-source packages) in Product Documentation.

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.