Zig Requires Chilkat v11.7.0+
Zig
Remove a PDF's Old Revisions
See more PDF Optimization Examples
Demonstrates dropOldRevisions. A PDF that has been edited and saved several times carries every earlier revision: the superseded objects are still in the file, reachable through the older cross-reference sections. The audit reports them as "oldRevisions", and dropping them is often the single largest saving in a real document. Dropping them requires a full rewrite, which is why it invalidates existing digital signatures.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// How much of this file is superseded revisions?
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.optimize("audit", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("fileSize: {d}\n", .{json_out.intOf("fileSize")});
std.debug.print("oldRevisions: {d}\n", .{json_out.intOf("usage.oldRevisions.bytes")});
// Drop them, and nothing else, so that the saving is attributable.
json.updateBool("compressUncompressed", false) catch {};
json.updateBool("recompressFlate", false) catch {};
json.updateBool("replaceLegacyFilters", false) catch {};
json.updateBool("imagePredictors", false) catch {};
json.updateBool("dedupeStreams", false) catch {};
json.updateBool("dedupeObjects", false) catch {};
json.updateBool("removeUnreferenced", false) catch {};
json.updateBool("removeUnusedResources", false) catch {};
json.updateBool("removeUnusedLayers", false) catch {};
json.updateBool("coalesceContent", false) catch {};
json.updateBool("tidyContent", false) catch {};
json.updateBool("subsetFonts", false) catch {};
json.updateBool("dropOldRevisions", true) catch {};
pdf.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
pdf.writeFile("qa_output/grand_canyon_one_revision.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// The saving is an estimate: what the old revisions occupied is known exactly, but the writer
// decides the final layout. Measure the written file for the real number.
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
std.debug.print("bytesSaved (reported): {d}\n", .{json_out.intOf("bytesSaved")});
std.debug.print("written file: {d}\n", .{fac.fileSize("qa_output/grand_canyon_one_revision.pdf")});
}