Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
fac: HCkFileAccess;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
pdf1: HCkPdf;
pdf2: HCkPdf;
pdf3: HCkPdf;
begin
success := False;
// 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
fac := CkFileAccess_Create();
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
// generate: pack the objects (the default).
pdf1 := CkPdf_Create();
success := CkPdf_LoadFile(pdf1,'qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
Exit;
end;
CkJsonObject_UpdateString(json,'objectStreams','generate');
success := CkPdf_Optimize(pdf1,'optimize',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
Exit;
end;
success := CkPdf_WriteFile(pdf1,'qa_output/grand_canyon_objstm_generate.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf1));
Exit;
end;
Memo1.Lines.Add('generate: ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/grand_canyon_objstm_generate.pdf')) + ' bytes, objects packed: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'actions.objectStreams.count')));
// disable: every object on its own, with a classic cross-reference table.
pdf2 := CkPdf_Create();
success := CkPdf_LoadFile(pdf2,'qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
Exit;
end;
CkJsonObject_UpdateString(json,'objectStreams','disable');
success := CkPdf_Optimize(pdf2,'optimize',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
Exit;
end;
success := CkPdf_WriteFile(pdf2,'qa_output/grand_canyon_objstm_disable.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
Exit;
end;
Memo1.Lines.Add('disable: ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/grand_canyon_objstm_disable.pdf')) + ' bytes');
// preserve: leave the file's own structure alone. Use it when the output must stay readable by
// whatever reads the input today.
pdf3 := CkPdf_Create();
success := CkPdf_LoadFile(pdf3,'qa_data/pdf/grand_canyon_hiking_guide.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf3));
Exit;
end;
CkJsonObject_UpdateString(json,'objectStreams','preserve');
success := CkPdf_Optimize(pdf3,'optimize',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf3));
Exit;
end;
success := CkPdf_WriteFile(pdf3,'qa_output/grand_canyon_objstm_preserve.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf3));
Exit;
end;
Memo1.Lines.Add('preserve: ' + IntToStr(CkFileAccess_FileSize(fac,'qa_output/grand_canyon_objstm_preserve.pdf')) + ' bytes');
CkFileAccess_Dispose(fac);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(pdf1);
CkPdf_Dispose(pdf2);
CkPdf_Dispose(pdf3);