Foxit PDF SDK for Windows

How to optimize PDFs with Foxit PDF SDK

Foxit PDFOptimizer Add-on

Optimization feature can reduce the size of PDF files to save disk space and make files easier to send and store, through compressing images, deleting redundant date, discarding useless user data and so on. In version 7.0, the optimization module provides functions to compress the color/grayscale/monochrome images in PDF files to reduce the size of the PDF files.

Note: To use the Optimization feature, please make sure the license key has the permission of the ‘Optimization’ module.

Example:

How to optimize PDF files by compressing the color/grayscale/monochrome images

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/addon/optimization/fs_optimization.h"

using namespace std;
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace foxit::addon;

PDFDoc doc("input_pdf_file");
ErrorCode error_code = doc.Load();
if (error_code != foxit::e_ErrSuccess) {
printf("Error: %d\n", error_code);
  return 1;
}
Optimization_Pause pause(0,true);
addon::optimization::OptimizerSettings settings;
common::Progressive progressive = addon::optimization::Optimizer::Optimize(doc,settings,&pause);
cout << "Optimized Start." << endl;
Progressive::State progress_state = Progressive::e_ToBeContinued;
while (Progressive::e_ToBeContinued == progress_state) {
progress_state = progressive.Continue();
  int percent = progressive.GetRateOfProgress();
String res_string;
res_string.Format("Optimize progress percent: %d %",percent);
std::cout<<res_string<<std::endl;
}
if(Progressive::e_Finished == progress_state)
{
doc.SaveAs(L"ImageCompression_Optimized.pdf", foxit::pdf::PDFDoc::e_SaveFlagRemoveRedundantObjects);
}
cout << "Optimized Finish." << endl;

 

Updated on September 11, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: