0

I study preparing a dictionary programme with delphi. So far I have solved my problems about Word documents but I've got some problem about PDF documents. I imported and installed the AcroPdf component with Delphi 7 and I want to get the word (or text) which was selected by dblclicking by user from pdf document which was viewed by the ACROPDF component in Delphi. If I can get it I'll send it the dictionary database directly. If you help me I'll be glad. Thank you... Remzi MAKAK

  • Are you using the full version of Adobe Acrobat or the Reader? – MartynA Apr 26 '16 at 15:17
  • Yes, I use Adob Acrobat X pro full version. – Remzi MAKAK May 24 '16 at 19:54
  • Dear friends don't you know anything about this subject? – Remzi MAKAK Jun 02 '16 at 08:47
  • Mr. MartynA is there any releationship with using full version of Adobe Acrobat? – Remzi MAKAK Jun 06 '16 at 08:48
  • I'm not sure what you mean by "any relationship". My answer does what I understand you to be asking, using Acrobat's automation library. I'm afraid I don't know anything about axacropdf. – MartynA Jun 06 '16 at 12:05
  • Mr.MartynA, thank you very much for your answer. I've got Acrobat X Pro full version and I use Delphi 7. I have imported AcroPdfLib_Tlb.Pas from Acrobat X and compiled it as AcroPdf component. But it doesn't let me onclick event and it doesn't me to get selected text. – Remzi MAKAK Jun 06 '16 at 13:57
  • I don;t have version X of Acrobat but I am wondering whether we are talking about the same Acrobat import unit. Does your AcroPdfLib_Tlb.Pas contain declarations of interfaces such as CAcroApp and CAcroAVDoc? If not, you need to import that unit using D7's Import facility. – MartynA Jun 06 '16 at 14:53
  • "But it doesn't let me onclick event and it doesn't me to get selected text." The OnClick event should be on your project's Main Form, not in Acrobat. I've updated my answer to include a bit more detail of exactly what to do. – MartynA Jun 06 '16 at 15:28
  • " AcroPdfLib_Tlb.Pas " Btw, I think this may be the **wrong** file to import. The name resembles the import unit which was generated from the Acrobat **Reader** program in older versions of Acrobat. – MartynA Jun 08 '16 at 09:31

1 Answers1

1

The following shows one way to get the selected text from a Pdf document which is open in Adobe Acrobat Professional (v.8, English version).

Update The original version of this answer neglected to check the Boolean result of calling MenuItemExecute and specified the wrong argument to it. Both these points are fixed in the updated version of this answer. It turned out that the reason the call to MenuItemExecute was failing was that it is essential to call BringToFront on the Acrobat document before trying to copy the text selected in to to the clipboard.

  1. Create a new Delphi VCL project.

  2. In D7's IDE go to Projects | Import Type Library, and in the Import Type Library pop-up, scroll down until you see something like "Acrobat (Version 1.0) in the list of files, and "TAcroApp, TAcroAVDoc..." in the Class names box. That is the one you need to import. Click the Create unit button/

  3. In the project's main form file

    a. Make sure it USES the Acrobat_Tlb.Pas unit from step 2. You may need to add the path to wherever you saved Acrobat_Tlb.Pas to the SearchPath of your project.

    b. Drop a TButton on the form, name it btnGetSel. Drop a TEdit on the form and name it edSelection

  4. Edit the source code of your main form unit as shown below.

  5. Set a debugger breakpoint on Acrobat.MenuItemExecute('File->Copy'); Do not set a breakpoint within the GetSelection procedure as this is likely to defeat the call to BringToFront in it.

  6. Close any running instance of Adobe Acrobat. Check in Task Manager that there are no hidden instances of it running. The reason for these step is to make sure that when you run your app, it "talks" to the instance of Acrobat that it starts, not another one.

  7. Compile and run your app. Once the app and Acrobat are open, switch to Acrobat, select some text, switch back to your app and click the btnGetSel button.

Code:

  uses ... Acrobat_Tlb, ClipBrd;

  TDefaultForm = class(TForm)
  [...]
  private
    FFileName: String;
    procedure GetSelection;
  public
    Acrobat : CAcroApp;
    PDDoc : CAcroPDDoc;
    AVDoc : CAcroAVDoc;
  end;

[...]

procedure TDefaultForm.FormCreate(Sender: TObject);
begin
  //  Adjust the following path to suit your system.  My application is
  //  in a folder on drive D:
  FFileName := ExtractfilePath(Application.ExeName) + 'Printed.Pdf';
  Acrobat := CoAcroApp.Create;
  Acrobat.Show;
  AVDoc := CoAcroAVDoc.Create;
  AVDoc.Open(FileName, FileName); // := Acrobat.GetAVDoc(0) as CAcroAVDoc; //

  PDDoc := AVDoc.GetPDDoc as CAcroPDDoc;
end;

procedure TDefaultForm.btnGetSelClick(Sender: TObject);
begin
  GetSelection;
end;

procedure TDefaultForm.GetSelection;
begin
  // call this once some text is selected in Acrobat
  edSelection.Text := '';

  if AVDoc.BringToFront then  //  NB:  This call to BringToFront is essential for the call to MenuItemExecute('Copy') to succeed 
    Caption := 'BringToFront ok'
  else
    Caption := 'BringToFront failed';
  if Acrobat.MenuItemExecute('Copy') then
    Caption := 'Copy ok'
  else
    Caption := 'BringToFront failed';

  Sleep(100);  // Normally I would avoid ever calling Sleep in a Delphi     
  //  App's main thread.  In this case, it is to allow Acrobat time to transfer the selected
  //  text to the clipboard before we attempt to read it.

  try
    edSelection.Text := Clipboard.AsText;
  except
  end;

end;
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Sorry, there was a typo in "Acrobat_Tlb" when I first hand-edited it into the Uses list. Fixed. – MartynA Jun 06 '16 at 14:49
  • Mr.MartynA, thankyou very much for your help. I have got your answer, Now I'm trying to change it Acrobat XI, when I get result I am going to inform you. – Remzi MAKAK Jun 07 '16 at 08:23
  • Mr.MartynA, I tested the program codes with Acrobat XI pro full version but I couldn't get the success. I realized that ACROBAT.MENUITEMEXECUTE('FILE->COPY') doesn't work. – Remzi MAKAK Jun 08 '16 at 11:02
  • a) Does the code in my answer compile for your? b) Is your version of Acrobat in English or some other language? c) If it is another language, have you tried using the translation of "File->Copy"? – MartynA Jun 08 '16 at 11:06
  • Mr.MartynA; I have written the program and compiled succesfully. My Acrobat version is in Turkish and I have tried using the translation of "File->Copy" but it doesn't work successfully. Instead of using Acrobat.Menuitemexecute(..) If I use CTRL+C with the keyboard it gave me the selected text from clipboard but I need to get selected text with delphi code. – Remzi MAKAK Jun 08 '16 at 13:11
  • Please see update, which now correctly works for me with the English version of Acrobat. I'm not sure if you need to translate "Copy" into Turkish to get it to work. Please try both and let me know how you get on – MartynA Jun 08 '16 at 18:58
  • Mr. MartynA, Thankyou very much for your help again. The last update is OK. – Remzi MAKAK Jun 09 '16 at 10:53
  • Mr. MartynA, after getting success with your update information I have tried AVDoc.OpenInWindowEx(FFilename, Panel1.handle, 0, True, 0,0, 2, 0, 0, 0); but it doesn't work successfully. I wonder It doesn't let us to use menuitemexecute() function. – Remzi MAKAK Jun 09 '16 at 12:46
  • I am glad you got it working. Would you mind accepting my answer, please, as I think it answers the question you originally asked and took me an entire day to get it to work. I think you had better ask about getting MenuItemExecute to work with AVDoc.OpenInWindowEX in a new question, because it is a separate problem. Seeing as getting MenuItemExecute('Copy') requires a preceding call to BringToFront, I don't see how you could get that to work when the doc is being hosted in a non-Acrobat window. – MartynA Jun 09 '16 at 13:03
  • Ok. Mr.Martyn. I accept your answer. But I don't know if there is anything to do in the form principles to accept the answer. If you help me I'll be glad. As you said I am going to ask a new question about last situation. – Remzi MAKAK Jun 09 '16 at 13:20
  • Sorry, when I said "accept", I meant in the SO sense. In other words, you click the "tick" icon at the top left of the answer, and the SO s/ware marks the answer as accepted. – MartynA Jun 09 '16 at 13:24
  • I followed all steps, but I get an error undeclared identifier "CAcroApp" – Rati2019 Mar 27 '20 at 21:23
  • Sorry, but you obviously didn't follow all the steps correctly, otherwise you wouldn't get that error. – MartynA Mar 27 '20 at 21:28