Zig Requires Chilkat v11.7.0+
Zig
Deflate Compression Level When Optimizing a PDF
See more PDF Optimization Examples
Demonstrates compressionLevel, which is the deflate level (1 to 9, 9 by default) used for every stream the optimizer re-encodes. Level 9 is the smallest and the slowest; level 1 is the quickest. The difference is usually a percent or two, so level 9 is the right default for a file that will be downloaded many times, and a lower level is worth considering only when a server optimizes PDFs on the fly. A value outside 1 to 9 is an error.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 watersense_accomplishments.pdf
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
const pdf1 = try chilkat.Pdf.init();
defer pdf1.deinit();
pdf1.loadFile("qa_data/pdf/watersense_accomplishments.pdf") catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
json.updateInt("compressionLevel", 1) catch {};
pdf1.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
pdf1.writeFile("qa_output/watersense_level1.pdf") catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
std.debug.print("level 1: {d}\n", .{fac.fileSize("qa_output/watersense_level1.pdf")});
const pdf9 = try chilkat.Pdf.init();
defer pdf9.deinit();
pdf9.loadFile("qa_data/pdf/watersense_accomplishments.pdf") catch {
std.debug.print("{s}\n", .{try pdf9.getLastErrorText(alloc)});
return;
};
json.updateInt("compressionLevel", 9) catch {};
pdf9.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf9.getLastErrorText(alloc)});
return;
};
pdf9.writeFile("qa_output/watersense_level9.pdf") catch {
std.debug.print("{s}\n", .{try pdf9.getLastErrorText(alloc)});
return;
};
std.debug.print("level 9: {d}\n", .{fac.fileSize("qa_output/watersense_level9.pdf")});
// A level outside 1..9 fails, and LastErrorText says why.
const pdf_bad = try chilkat.Pdf.init();
defer pdf_bad.deinit();
pdf_bad.loadFile("qa_data/pdf/watersense_accomplishments.pdf") catch {
std.debug.print("{s}\n", .{try pdf_bad.getLastErrorText(alloc)});
return;
};
json.updateInt("compressionLevel", 12) catch {};
pdf_bad.optimize("optimize", json, json_out) catch {
std.debug.print("compressionLevel 12 was refused, as it should be.\n", .{});
};
}