Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
Deflate Compression Level When Optimizing a PDF
See more PDF Optimization Examples
Demonstrates compressionLevel, which is the deflate level (1 to 9, 9 by default) used for every stream the optimizer re-encodes. Level 9 is the smallest and the slowest; level 1 is the quickest. The difference is usually a percent or two, so level 9 is the right default for a file that will be downloaded many times, and a lower level is worth considering only when a server optimizes PDFs on the fly. A value outside 1 to 9 is an error.Chilkat Unicode C++ Downloads
#include <CkFileAccessW.h>
#include <CkJsonObjectW.h>
#include <CkPdfW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at watersense_accomplishments.pdf
CkFileAccessW fac;
CkJsonObjectW json;
CkJsonObjectW jsonOut;
CkPdfW pdf1;
success = pdf1.LoadFile(L"qa_data/pdf/watersense_accomplishments.pdf");
if (success == false) {
wprintf(L"%s\n",pdf1.lastErrorText());
return;
}
json.UpdateInt(L"compressionLevel",1);
success = pdf1.Optimize(L"optimize",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf1.lastErrorText());
return;
}
success = pdf1.WriteFile(L"qa_output/watersense_level1.pdf");
if (success == false) {
wprintf(L"%s\n",pdf1.lastErrorText());
return;
}
wprintf(L"level 1: %d\n",fac.FileSize(L"qa_output/watersense_level1.pdf"));
CkPdfW pdf9;
success = pdf9.LoadFile(L"qa_data/pdf/watersense_accomplishments.pdf");
if (success == false) {
wprintf(L"%s\n",pdf9.lastErrorText());
return;
}
json.UpdateInt(L"compressionLevel",9);
success = pdf9.Optimize(L"optimize",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf9.lastErrorText());
return;
}
success = pdf9.WriteFile(L"qa_output/watersense_level9.pdf");
if (success == false) {
wprintf(L"%s\n",pdf9.lastErrorText());
return;
}
wprintf(L"level 9: %d\n",fac.FileSize(L"qa_output/watersense_level9.pdf"));
// A level outside 1..9 fails, and LastErrorText says why.
CkPdfW pdfBad;
success = pdfBad.LoadFile(L"qa_data/pdf/watersense_accomplishments.pdf");
if (success == false) {
wprintf(L"%s\n",pdfBad.lastErrorText());
return;
}
json.UpdateInt(L"compressionLevel",12);
success = pdfBad.Optimize(L"optimize",json,jsonOut);
if (success == false) {
wprintf(L"compressionLevel 12 was refused, as it should be.\n");
}
}