Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

Where a PDF's Bytes Go: the Optimize Audit

See more PDF Optimization Examples

Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Pdf,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  est: TJsonObject;
  i: Integer;
  numEst: Integer;
  optionName: string;

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

  pdf := TPdf.Create;
  success := pdf.LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  "audit" takes the same options as "optimize", so an empty JSON means the default options.
  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  success := pdf.Optimize('audit',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  WriteLn('fileSize: ' + jsonOut.IntOf('fileSize'));
  WriteLn('pdfVersion: ' + jsonOut.StringOf('pdfVersion'));
  WriteLn('pdfa: ' + jsonOut.StringOf('pdfa'));

  //  Where the bytes go.  Every category is present even when it is 0, and the sizes are the stored
  //  (compressed) sizes, so they add up to fileSize.  "overhead" is what is left: the cross-reference
  //  tables, the object headers and the white space between objects.
  WriteLn('images:        ' + jsonOut.IntOf('usage.images.bytes') + ' in ' + jsonOut.IntOf('usage.images.count') + ' objects');
  WriteLn('fonts:         ' + jsonOut.IntOf('usage.fonts.bytes') + ' in ' + jsonOut.IntOf('usage.fonts.count') + ' objects');
  WriteLn('pageContent:   ' + jsonOut.IntOf('usage.pageContent.bytes'));
  WriteLn('formXObjects:  ' + jsonOut.IntOf('usage.formXObjects.bytes'));
  WriteLn('annotations:   ' + jsonOut.IntOf('usage.annotations.bytes'));
  WriteLn('structureTree: ' + jsonOut.IntOf('usage.structureTree.bytes'));
  WriteLn('metadata:      ' + jsonOut.IntOf('usage.metadata.bytes'));
  WriteLn('embeddedFiles: ' + jsonOut.IntOf('usage.embeddedFiles.bytes'));
  WriteLn('oldRevisions:  ' + jsonOut.IntOf('usage.oldRevisions.bytes'));
  WriteLn('overhead:      ' + jsonOut.IntOf('usage.overhead.bytes'));

  //  What each option is expected to save.  An option that would save nothing is left out.
  WriteLn('estimated total: ' + jsonOut.IntOf('estimatedSavings.total'));

  est := TJsonObject.Create;
  success := jsonOut.ObjectOf2('estimatedSavings',est);
  if (success = False) then
    begin
      WriteLn(jsonOut.LastErrorText);
      Exit;
    end;

  i := 0;
  numEst := est.Size;
  while i < numEst do
    begin
      optionName := est.NameAt(i);
      WriteLn('   ' + optionName + ': ' + est.IntOf(optionName));
      i := i + 1;
    end;



  pdf.Free;
  json.Free;
  jsonOut.Free;
  est.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.