Zig Requires Chilkat v11.7.0+
Zig
PDF Object Streams: generate, preserve or disable
See more PDF Optimization Examples
Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.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 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();
// generate: pack the objects (the default).
const pdf1 = try chilkat.Pdf.init();
defer pdf1.deinit();
pdf1.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf") catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
json.updateString("objectStreams", "generate") catch {};
pdf1.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
pdf1.writeFile("qa_output/grand_canyon_objstm_generate.pdf") catch {
std.debug.print("{s}\n", .{try pdf1.getLastErrorText(alloc)});
return;
};
std.debug.print("generate: {d} bytes, objects packed: {d}\n", .{ fac.fileSize("qa_output/grand_canyon_objstm_generate.pdf"), json_out.intOf("actions.objectStreams.count") });
// disable: every object on its own, with a classic cross-reference table.
const pdf2 = try chilkat.Pdf.init();
defer pdf2.deinit();
pdf2.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf") catch {
std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
return;
};
json.updateString("objectStreams", "disable") catch {};
pdf2.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
return;
};
pdf2.writeFile("qa_output/grand_canyon_objstm_disable.pdf") catch {
std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
return;
};
std.debug.print("disable: {d} bytes\n", .{fac.fileSize("qa_output/grand_canyon_objstm_disable.pdf")});
// preserve: leave the file's own structure alone. Use it when the output must stay readable by
// whatever reads the input today.
const pdf3 = try chilkat.Pdf.init();
defer pdf3.deinit();
pdf3.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf") catch {
std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
return;
};
json.updateString("objectStreams", "preserve") catch {};
pdf3.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
return;
};
pdf3.writeFile("qa_output/grand_canyon_objstm_preserve.pdf") catch {
std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
return;
};
std.debug.print("preserve: {d} bytes\n", .{fac.fileSize("qa_output/grand_canyon_objstm_preserve.pdf")});
}