Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Exclude Resources Matching a Pattern

See more MHT / HTML Email Examples

Demonstrates Mht.ExcludeImagesMatching, which adds an exclusion pattern used while gathering embeddable resources, so matching resources are omitted from the archive.

Background. Despite the method name, the test applies to embeddable resources generally, not only images. Each candidate is tested against its fully resolved reference — an absolute URL for web content, or the resolved local path for a local resource.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Mht;

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

procedure RunDemo;
var
  mht: TMht;
  mhtText: string;

begin
  mht := TMht.Create;

  //  Add an exclusion pattern for embeddable resources.  Each candidate resource is tested against its
  //  fully resolved reference (an absolute URL for web content), so resources whose reference matches
  //  the pattern are omitted from the archive.  Here, resources ending in ".gif" are excluded.
  mht.ExcludeImagesMatching('*.gif');

  //  Create the MHT web archive without the excluded resources.
  mhtText := mht.GetMHT('https://example-code.com/crumb/index.html');
  if (mht.LastMethodSuccess = False) then
    begin
      WriteLn(mht.LastErrorText);
      Exit;
    end;
  WriteLn(mhtText);


  mht.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.