Lua Requires Chilkat v11.7.0+
Lua
Optimize a PDF Without Rewriting It (Incremental Update)
See more PDF Optimization Examples
Demonstrates the one combination of options that does not rewrite the file. With dropOldRevisions false and objectStreams "disable", the optimized objects are appended to the original bytes as an incremental update: every byte of the input is still there, byte for byte, followed by the changes and a new cross-reference section. That is what keeps an existing digital signature valid, and what lets a reader that has the original cached fetch only the tail. The file gets a little larger before it gets smaller, so this is for compatibility, not for size.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/watersense_accomplishments.pdf")
if not success then
print(pdf.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json:update_bool("dropOldRevisions", false)
json:update_string("objectStreams", "disable")
local jsonOut = chilkat.JsonObject.new()
success = pdf:optimize("optimize", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
success = pdf:write_file("qa_output/watersense_incremental.pdf")
if not success then
print(pdf.last_error_text)
return
end
local fac = chilkat.FileAccess.new()
print("original: " .. jsonOut:int_of("fileSize"))
print("written: " .. fac:file_size("qa_output/watersense_incremental.pdf"))
-- Everything the optimizer changed is in the appended part; the original bytes are the start of the
-- new file. Any other combination of options -- dropOldRevisions, or generated object streams --
-- rewrites the document instead.
print("bytesSaved: " .. jsonOut:int_of("bytesSaved"))