Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.7.0+

Shrink a PDF Losslessly

See more PDF Optimization Examples

Demonstrates the "optimize" operation with the default options, which is the lossless preset: nothing is resampled, recoloured or thrown away, so every page looks exactly as it did and all the text is still there. The changes are kept in memory until WriteFile (or WriteBd) is called. Note that the real saving is "fileSize" minus the size of the file that was written, not "bytesSaved": the rewrite also packs objects into object streams and writes a compact cross-reference stream, which belongs to no option.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loFac
LOCAL lnSizeBefore
LOCAL lnSizeAfter

lnSuccess = 0

*  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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

*  An empty JSON means the default options: every lossless optimization, object streams generated,
*  old revisions dropped, deflate level 9.
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Optimize("optimize",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  Nothing has been written yet.  WriteFile (or WriteBd) produces the optimized PDF.
lnSuccess = loPdf.WriteFile("qa_output/grand_canyon_optimized.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

loFac = CreateObject('Chilkat.FileAccess')
lnSizeBefore = loJsonOut.IntOf("fileSize")
lnSizeAfter = loFac.FileSize("qa_output/grand_canyon_optimized.pdf")

? "before: " + STR(lnSizeBefore)
? "after:  " + STR(lnSizeAfter)
? "saved:  " + STR(lnSizeBefore - lnSizeAfter)

*  What the options themselves account for.  This is smaller than the real saving above, because the
*  rewrite's own gains (object streams, the cross-reference stream, the old sections left behind)
*  belong to no single option.
? "bytesSaved (options only): " + STR(loJsonOut.IntOf("bytesSaved"))

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loFac