Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Optimizing a Signed PDF (PreserveSignatures)
See more PDF Optimization Examples
Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.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.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
pdf2: TPdf;
sigInfo: TJsonObject;
verified: Boolean;
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 invoice_signed.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/invoice_signed.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('signatures: ' + pdf.NumSignatures);
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
// An audit changes nothing, so it is allowed on a signed PDF.
success := pdf.Optimize('audit',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('audit ok, estimated savings: ' + jsonOut.IntOf('estimatedSavings.total'));
// Optimizing is refused while PreserveSignatures is true.
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn('Refused while PreserveSignatures is true.');
end;
// Allow the rewrite. Do this only when the signatures no longer matter -- an invoice being
// archived, say, where the signed original is kept elsewhere.
pdf.PreserveSignatures := False;
success := pdf.Optimize('optimize',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
success := pdf.WriteFile('qa_output/invoice_optimized.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('optimized, bytesSaved: ' + jsonOut.IntOf('bytesSaved'));
// The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
pdf2 := TPdf.Create;
success := pdf2.LoadFile('qa_output/invoice_optimized.pdf');
if (success = False) then
begin
WriteLn(pdf2.LastErrorText);
Exit;
end;
WriteLn('signatures after: ' + pdf2.NumSignatures);
sigInfo := TJsonObject.Create;
verified := pdf2.VerifySignature(0,sigInfo);
WriteLn('verifies after optimizing: ' + verified);
pdf.Free;
json.Free;
jsonOut.Free;
pdf2.Free;
sigInfo.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.