Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.FileAccess,
Chilkat.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
fac: TFileAccess;
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 watersense_accomplishments.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/watersense_accomplishments.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
json := TJsonObject.Create;
json.UpdateBool('dropOldRevisions',False);
json.UpdateString('objectStreams','disable');
jsonOut := TJsonObject.Create;
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/watersense_incremental.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
fac := TFileAccess.Create;
WriteLn('original: ' + jsonOut.IntOf('fileSize'));
WriteLn('written: ' + fac.FileSize('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.
WriteLn('bytesSaved: ' + jsonOut.IntOf('bytesSaved'));
pdf.Free;
json.Free;
jsonOut.Free;
fac.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.