PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Fac
long li_SizeBefore
long li_SizeAfter
li_Success = 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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
// An empty JSON means the default options: every lossless optimization, object streams generated,
// old revisions dropped, deflate level 9.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Pdf.Optimize("optimize",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// Nothing has been written yet. WriteFile (or WriteBd) produces the optimized PDF.
li_Success = loo_Pdf.WriteFile("qa_output/grand_canyon_optimized.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
li_SizeBefore = loo_JsonOut.IntOf("fileSize")
li_SizeAfter = loo_Fac.FileSize("qa_output/grand_canyon_optimized.pdf")
Write-Debug "before: " + string(li_SizeBefore)
Write-Debug "after: " + string(li_SizeAfter)
Write-Debug "saved: " + string(li_SizeBefore - li_SizeAfter)
// 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.
Write-Debug "bytesSaved (options only): " + string(loo_JsonOut.IntOf("bytesSaved"))
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Fac