PDF is a standard format for sharing documents over the internet. Its global support and consistent layout make it an ideal choice for such scenarios. However, sometimes, the size of PDF files tends to grow due to contents like images, drawings, etc. In such scenarios, you can easily optimize or compress PDF files to reduce their size without compromising their quality. To that end, this article will teach you different techniques that you can use to compress and optimize PDF files to reduce their size using C++.
- C++ API for Compressing and Optimizing PDF Files
- Optimize PDF Documents for the Web using C++
- Compress and Resize Images in PDF Files using C++
- Remove Embedded Fonts, Unused Streams, and Link Duplicate Streams using C++
- Remove or Flatten Annotations using C++
- Removing Form Fields from PDF Files using C++
- Convert RGB Color Space to Grayscale for PDF Files using C++
C++ API for Compressing and Optimizing PDF Files
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. You can use this API to compress and optimize large PDF files to reduce their size while still retaining their quality. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Optimize PDF Documents for the Web using C++
If you want to display the PDF Documents on the web, optimizing them will improve their loading speed and thus enhance the user experience. In order to optimize PDF documents, please follow the steps given below.
- Load the PDF document using the Document class.
- Optimize the document using the Document->Optimize() method.
- Save the PDF document using the Document->Save(System::String outputFileName) method.
The following sample code shows how to optimize PDF documents for the web using C++.
Compress and Resize Images in PDF Files using C++
If PDF files contain a large number of images, that can result in substantial file sizes. In such files, images play a significant role in increasing their size. Compressing and resizing images in these files would result in noticeable reduction of their sizes. The following are the steps to compress and resize images in a PDF file.
- Load the PDF file using the Document class.
- Create an instance of the OptimizationOptions class.
- Set the compression, image quality, and resolution options of the OptimizationOptions object.
- Use the Document->OptimizeResources(System::SharedPtrAspose::Pdf::Optimization::OptimizationOptions strategy) method to optimize the resources.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code demonstrates how to compress and resize images to reduce PDF file size using C++.
Remove Embedded Fonts, Unused Streams, and Link Duplicate Streams using C++
To further reduce the file size, you can also remove embedded fonts from the file. There are two strategies for removing embedded fonts; the first is that you remove all embedded fonts, and the second is that you only remove the subset of unused fonts. Furthermore, you can also remove unused streams and link duplicate streams to reduce additional size. The following are the steps to apply previously discussed changes to a PDF file.
- Load the PDF file using the Document class.
- Create an instance of the OptimizationOptions class.
- Either unembed all fonts or the subset of fonts.
- Link duplicate streams.
- Remove unused streams and objects.
- Use the Document->OptimizeResources(System::SharedPtrAspose::Pdf::Optimization::OptimizationOptions strategy) method to optimize the resources.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to reduce PDF file size by unembedding fonts and removing unused streams and objects using C++.
Remove or Flatten Annotations using C++
There can be multiple annotations in PDF files like text, shapes, etc., that increase the file size. These annotations can be removed if they are no longer required or flattened if no further changes are needed. Doing this will reduce the size of the PDF file. The following are the steps to remove or flatten annotations from PDF files.
- Create an instance of the PdfAnnotationEditor class.
- Load the source PDF file using the PdfAnnotationEditor->BindPdf (System::SharedPtrAspose::Pdf::Document srcDoc) method.
- Flatten or delete the annotations.
- Save the PDF file using the PdfAnnotationEditor->Save(System::String destFile) method.
The following sample code shows how to delete or flatten annotations in PDF files using C++.
Removing Form Fields from PDF Files using C++
Forms fields are required when you need to collect data. If data collection is no longer required, you can flatten the form fields to reduce the PDF file size. The following are the steps to flatten form fields in PDF files.
- Load the PDF file using the Document class.
- Check for the existence of form fields.
- Iterate through the form fields and flatten each field.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to flatten form fields in PDF files using C++.
Convert RGB Color Space to Grayscale for PDF Files using C++
PDF files with textual information can be represented well in grayscale color space, so when reducing the file size is essential, such PDF files can be converted to grayscale. Furthermore, suppose the priority is to archive files and to reduce the size as much as possible. In that case, PDF files with images can also be converted to grayscale as the primary objective is to make files as small as possible. To change the color space of PDF files from RGB to grayscale, follow the steps given below:
- Load the PDF file using the Document class.
- Create an instance of the RgbToDeviceGrayConversionStrategy class.
- Iterate through the pages of the PDF file.
- Use the RgbToDeviceGrayConversionStrategy->Convert (System::SharedPtr page) method to convert each page to grayscale.
- Save the PDF file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to convert the color space of a PDF file from RGB to grayscale using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
The target of this article was to look at different ways to compress and optimize PDF files to reduce their sizes. Firstly, we looked at how to optimize PDF files for the web. Then we saw how to compress and resize images in PDF files. Furthermore, we have learned how to remove embedded fonts and streams from PDF files. In addition to this, we learned how to flatten annotations and form fields, and convert the color space of PDF files to grayscale. We explored all these ways to reduce the size of the PDF files without affecting their quality. Aspose.PDF for C++ provides many additional features for working with PDF files. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us at our free support forum.