Unicode C Requires Chilkat v11.7.0+
Unicode C
Remove a PDF's Old Revisions
See more PDF Optimization Examples
Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.Chilkat Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkFileAccessW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
HCkFileAccessW fac;
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 grand_canyon_hiking_guide.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/grand_canyon_hiking_guide.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
// How much of this file is superseded revisions?
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_Optimize(pdf,L"audit",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"fileSize: %d\n",CkJsonObjectW_IntOf(jsonOut,L"fileSize"));
wprintf(L"oldRevisions: %d\n",CkJsonObjectW_IntOf(jsonOut,L"usage.oldRevisions.bytes"));
// Drop them, and nothing else, so that the saving is attributable.
CkJsonObjectW_UpdateBool(json,L"compressUncompressed",FALSE);
CkJsonObjectW_UpdateBool(json,L"recompressFlate",FALSE);
CkJsonObjectW_UpdateBool(json,L"replaceLegacyFilters",FALSE);
CkJsonObjectW_UpdateBool(json,L"imagePredictors",FALSE);
CkJsonObjectW_UpdateBool(json,L"dedupeStreams",FALSE);
CkJsonObjectW_UpdateBool(json,L"dedupeObjects",FALSE);
CkJsonObjectW_UpdateBool(json,L"removeUnreferenced",FALSE);
CkJsonObjectW_UpdateBool(json,L"removeUnusedResources",FALSE);
CkJsonObjectW_UpdateBool(json,L"removeUnusedLayers",FALSE);
CkJsonObjectW_UpdateBool(json,L"coalesceContent",FALSE);
CkJsonObjectW_UpdateBool(json,L"tidyContent",FALSE);
CkJsonObjectW_UpdateBool(json,L"subsetFonts",FALSE);
CkJsonObjectW_UpdateBool(json,L"dropOldRevisions",TRUE);
success = CkPdfW_Optimize(pdf,L"optimize",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
success = CkPdfW_WriteFile(pdf,L"qa_output/grand_canyon_one_revision.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// The saving is an estimate: what the old revisions occupied is known exactly, but the writer
// decides the final layout. Measure the written file for the real number.
fac = CkFileAccessW_Create();
wprintf(L"bytesSaved (reported): %d\n",CkJsonObjectW_IntOf(jsonOut,L"bytesSaved"));
wprintf(L"written file: %d\n",CkFileAccessW_FileSize(fac,L"qa_output/grand_canyon_one_revision.pdf"));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkFileAccessW_Dispose(fac);
}