C Requires Chilkat v11.7.0+
C
Remove PDF Objects Nothing Refers To
See more PDF Optimization Examples
Demonstrates removeUnreferenced. When a page, an image or a whole section is replaced in an editor, the objects it used often stay in the file with nothing pointing at them any more. They are not old revisions -- they are in the current revision, simply unreachable -- so only a reachability walk from the document catalog finds them. In a publication assembled over several editing passes this is frequently the largest single saving.Chilkat C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject jsonOut;
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
pdf = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/watersense_accomplishments.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
return;
}
json = CkJsonObject_Create();
jsonOut = CkJsonObject_Create();
success = CkPdf_Optimize(pdf,"audit",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("fileSize: %d\n",CkJsonObject_IntOf(jsonOut,"fileSize"));
printf("removeUnreferenced would save: %d\n",CkJsonObject_IntOf(jsonOut,"estimatedSavings.removeUnreferenced"));
// Optimize with the defaults and see how many objects were actually dropped.
success = CkPdf_Optimize(pdf,"optimize",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
success = CkPdf_WriteFile(pdf,"qa_output/watersense_optimized.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("objects removed: %d\n",CkJsonObject_IntOf(jsonOut,"actions.removeUnreferenced.count"));
printf("bytes saved: %d\n",CkJsonObject_IntOf(jsonOut,"actions.removeUnreferenced.bytesSaved"));
// Nothing a page needs is ever removed: the walk starts at the document catalog and follows every
// reference, including the ones in annotations, the structure tree, named destinations and the
// optional-content configuration.
printf("pages after: %d\n",CkPdf_getNumPages(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}