DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoFac
Integer iSizeBefore
Integer iSizeAfter
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/grand_canyon_hiking_guide.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// An empty JSON means the default options: every lossless optimization, object streams generated,
// old revisions dropped, deflate level 9.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComOptimize Of hoPdf "optimize" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// Nothing has been written yet. WriteFile (or WriteBd) produces the optimized PDF.
Get ComWriteFile Of hoPdf "qa_output/grand_canyon_optimized.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComIntOf Of hoJsonOut "fileSize" To iSizeBefore
Get ComFileSize Of hoFac "qa_output/grand_canyon_optimized.pdf" To iSizeAfter
Showln "before: " iSizeBefore
Showln "after: " iSizeAfter
Showln "saved: " (iSizeBefore - iSizeAfter)
// 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.
Get ComIntOf Of hoJsonOut "bytesSaved" To iTemp1
Showln "bytesSaved (options only): " iTemp1
End_Procedure