0

I have importated Acrobate Reader ActiveX component in my Delphi application and loaded PDF document with code:

var AA: TAcroPDF;

function GetFilePath(AFileName: String): String;
begin
  Result:=IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+AFileName;
end;

procedure TMainForm.Open2BtnClick(Sender: TObject);
var FPath: String;
begin
  FPath:=GetFilePath('test.pdf');
  if not FileExists(FPath) then raise Exception.Create('No file');

  AA.LoadFile(FPath);
end; 

But I can not unload PDF file from Acrobat ActiveX component. My use case requires that component unloads the file and shows gray rectangle upon the request from user. I have tried several code lines:

AA.Src:='';
AA.LoadFile('');

But file stays loaded all the time. I can successfully call the load of another file, but I can not find the way to unload the file.

I am using Delphi 2009, but I guess that my question applies to any version.

Question update and suggestion of hack as a solution:

I managed to adapt the code from https://stackoverflow.com/a/37889375/1375882 as for the close:

procedure TMainForm.Close1BtnClick(Sender: TObject);
var
  Ref : Integer;
begin
  if Assigned(AA) then begin
    Ref:=AA.ControlInterface._AddRef;
    AA.Src:='';
    AA.Free;
    AA:=Nil;
  end;
end;

And accordingly I am checking if Assigned(AA) in FormClose as well. This works perfectly for me as far as I can see. But this seems to be no clean solution that was intended by Acrobat Reader?

Bad consequences of the suggested hack/solution:

No dark-gray rectangle remains on the form after onloading Acrobat Reader component in the suggested way and the subsequent call of anothes LoadFile(...) fails with AV, of course.

TomR
  • 2,696
  • 6
  • 34
  • 87

0 Answers0