program ExtractAnnotAttach;

{$I ..\..\..\Lib\PDFlibDefines.inc}

uses
{$IFDEF XE2+}
  System.SysUtils,
  Vcl.Dialogs,
{$ELSE}
  SysUtils,
  Dialogs,
{$ENDIF}
  PDFlibrary;

var
  PDF: TPDFlib;
  FileCount, FileIndex, Result: Integer;
  OutputFileName: WideString;

begin
  // Create an instance of PDF Library
  PDF := TPDFlib.Create;
  try
    // Load the PDF document
    Result := PDF.LoadFromFile('.\input.pdf', '');
    if Result = 0 then
    begin
      ShowMessage('Unable to load PDF file.');
      Exit;
    end;

    // Get the count of embedded files
    FileCount := PDF.EmbeddedFileCount;
    if FileCount = 0 then
    begin
      ShowMessage('No embedded files found.');
      Exit;
    end;

    // Iterate over all embedded files and extract them
    for FileIndex := 1 to FileCount do
    begin
      // Set output file name
      OutputFileName := Format('extracted_file_%d.xml', [FileIndex]);

      // Extract embedded file content to the specified path
      Result := PDF.GetEmbeddedFileContentToFile(FileIndex, OutputFileName);
      if Result = 1 then
        ShowMessage('Embedded file successfully extracted to: ' + OutputFileName)
      else
        ShowMessage('Failed to extract embedded file, file index: ' + IntToStr(FileIndex));
    end;

  finally
    // Release the PDF object
    PDF.Free;
  end;

end.
