Recent

Author Topic: Steal PDF preview out of TOpenDialog  (Read 8058 times)

eraaijma

  • New Member
  • *
  • Posts: 26
Steal PDF preview out of TOpenDialog
« on: August 25, 2015, 03:06:56 pm »
Hi Guys,

Using the Opendialog, I see a great preview of PDF's. I would like to be able to paint page -1- of a PDF on a canvas.
Does anyone know how to 'steal' the preview, or how to tap in to OpenDialog to get it?

Thanks,

Eugene


GetMem

  • Hero Member
  • *****
  • Posts: 3887
Re: Steal PDF preview out of TOpenDialog
« Reply #1 on: August 25, 2015, 08:04:14 pm »
You don't have to "steal" anything, just import Acrobat Browser Control type library into a component. After the component is installed,  drop a TAxcAcroPDF and a TButton to your form. Set TAxcAcroPDF Active property to True.
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  AxcAcroPDF1.OleServer.setPageMode('PAGE_MODE_USE_NONE');
  AxcAcroPDF1.OleServer.LoadFile(Utf8ToUtf16('PATH TO YOUR PDF'));
end;
Now you can set whatever page you like and draw the result to the desired canvas with AxcAcroPDf1.PaintTo method.

PS: I did imported the type library, all you have to do is copy AcroPDF(see attachment) to $(LazarusDir)\components\ then install the package.

rvk

  • Hero Member
  • *****
  • Posts: 4465
Re: Steal PDF preview out of TOpenDialog
« Reply #2 on: August 25, 2015, 09:04:31 pm »
But you need to have Acrobat reader installed for that, don't you?

How does Windows do it without Acrobat reader installed because nowadays it's not as usual that that's installed? There's probably a method to get the preview image out of the metadata Windows keeps stored.

Something similar to this:
http://stackoverflow.com/questions/2911837/how-to-retrieve-the-file-previews-used-by-windows-explorer-in-windows-vista-and
« Last Edit: August 25, 2015, 09:33:29 pm by rvk »

GetMem

  • Hero Member
  • *****
  • Posts: 3887
Re: Steal PDF preview out of TOpenDialog
« Reply #3 on: August 25, 2015, 09:39:19 pm »
Quote
But you need to have Acrobat reader installed for that, don't you?
Yes.

Quote
How does Windows do it without Acrobat reader installed because nowadays it's not as usual that that's installed? There's probably a method to get the preview image out of the metadata Windows keeps stored.
Take a look at Fortes Report RLPDFFilter.pas unit. It's really quite fascinating how a PDF file is created basically from nothing, using only the write/writeln functions(stream class). The reverse is also true, but  it's much more easy to rely on acrobat reader.

rvk

  • Hero Member
  • *****
  • Posts: 4465
Re: Steal PDF preview out of TOpenDialog
« Reply #4 on: August 25, 2015, 09:45:48 pm »
I just edited my response.
It would be easier to implement something like IExtractImage. That way you can also use the same for gif, pdf, mp4, mkv and all other formats for which Windows extracts a preview image.

Something similar to this:
http://stackoverflow.com/questions/2911837/how-to-retrieve-the-file-previews-used-by-windows-explorer-in-windows-vista-and

(I'm not behind my computer right now but if I am, I'll try to implement something like that, if it doesn't already exits for Lazarus.)

wp

  • Hero Member
  • *****
  • Posts: 8121
Re: Steal PDF preview out of TOpenDialog
« Reply #5 on: August 25, 2015, 09:52:53 pm »
The Delphi source code in archive.org mentioned in rvk's link works with Lazarus and does show me the preview image of a pdf file.

[EDIT] I had to "use" Windows, but did not check if cross-platform routines exist.
« Last Edit: August 25, 2015, 10:10:35 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

GetMem

  • Hero Member
  • *****
  • Posts: 3887
Re: Steal PDF preview out of TOpenDialog
« Reply #6 on: August 25, 2015, 09:57:57 pm »
Quote
@rvk
I just edited my response.
It would be easier to implement something like IExtractImage. That way you can also use the same for gif, pdf, mp4, mkv and all other formats for which Windows extracts a preview image.

Quote
@wp
The Delphi source code in archive.org mentioned in rvk's link works with Lazarus and does show me the preview image of a pdf file.

Cool!

rvk

  • Hero Member
  • *****
  • Posts: 4465
Re: Steal PDF preview out of TOpenDialog
« Reply #7 on: August 25, 2015, 10:52:12 pm »
Yeah, and like I suspected it works for all files. Movies, images, pdfs, everything for which explorer shows a preview image.

I'll attach the original code here before achive.org goes belly up or is temporarily offline. Besides adding Windows I also needed to add ShellApi at some point.

(and for the GDB crash I also needed to use GDB 7.7.1 or set DisableLoadSymbolsForLibraries to True. That TOpenDialog crash keeps on bugging me)

(note: The file attached is the original, loaded from archive.org and is for Delphi. You can convert the projects with the Delphi to Lazarus converter in Lazarus under Tools. Add Windows to the uses clause in shobjidlquot.pas and add Windows to the beginning of the uses clause in shellobjhelper.pas and ShellApi at the end of the uses clause. I tested the basic program, xtimgtst , and it works perfectly)

I did assume this question was asked for Windows OS. Cross-platform would be a whole other problem.

eraaijma

  • New Member
  • *
  • Posts: 26
Re: Steal PDF preview out of TOpenDialog
« Reply #8 on: August 26, 2015, 11:55:42 am »
@GetMem:
Thanks for your reply and library. Great idea to do it this way.
I tried to follow your steps. I ended up with a black canvas :(

This is the code I used:
Code: [Select]
if OpenDialog1.Execute then
begin
  AxcAcroPDF1.OleServer.setPageMode('PAGE_MODE_USE_NONE');
  AxcAcroPDF1.OleServer.LoadFile(Utf8ToAnsi(OpenDialog1.FileName));
  AxcAcroPDF1.OleServer.setPageMode('GotoFirstPage');
  AxcAcroPDF1.PaintTo(Image1.Canvas,0,0); 
end;

- I see that I have no function 'Uft8ToUtf16' so I used Utf8ToAnsi instead. Could this cause the problem?
- I think that somewhere I should point to page one of the PDF file. Is this ('GotoFirstPage') the right way to do it, or does this cause the canvas to be black?

Thanks in advance.


GetMem

  • Hero Member
  • *****
  • Posts: 3887
Re: Steal PDF preview out of TOpenDialog
« Reply #9 on: August 26, 2015, 06:33:57 pm »
@eraaijma
Quote
I see that I have no function 'Uft8ToUtf16' .
Add LazUTF8 to the uses clauses.

Quote
I think that somewhere I should point to page one of the PDF file. Is this ('GotoFirstPage') the right way to do it
AcroPDF.OleServer.SetCurrentPage(1);

Quote
Does this cause the canvas to be black?
No!

For a quick preview use the IExtractImage interface(see @rvk example). This is the proper way to do it. On the other hand, if you want to embed the pdf into your application, with zoom, page change, etc functionality go with AcroPDF(see attachment). I created the TAxcAcroPDF dynamically so you don't need the visual component.

eraaijma

  • New Member
  • *
  • Posts: 26
Re: Steal PDF preview out of TOpenDialog
« Reply #10 on: August 27, 2015, 10:10:37 pm »
@rvk
Thanks for your example code.
I have made two new, clean Lazarus installations, one 64-bit and one 32-bit

I have converted your code to Lazarus and got it to work on the 32-bit installation.
I did not get it to work on the 64-bit installation.
As it is on two physical machines, could I have messed up in the OS there?

The 64.bit exe works on no machine at all.
The 32-bit exe works on all machines, except on a windows10 laptop.

I have attached the 'business-end' of my code. In the end, I will only use the only-one function.

Please, could you tell me if 64-bit is a dead-end for this function?
Actually, I am hoping that you will see a giant mistake in my code.

Thanks for all your help


rvk

  • Hero Member
  • *****
  • Posts: 4465
Re: Steal PDF preview out of TOpenDialog
« Reply #11 on: August 28, 2015, 10:59:56 am »
Please, could you tell me if 64-bit is a dead-end for this function?
I imagine the explorer.exe in Windows 64-bit is also 64-bit and uses IExtractImage so using 64-bit shouldn't be a dead-end.

I'm on Windows 10 64-bit but I don't see pdf-content in my thumbnails. I do see them in the preview screen but that's a different process (I think).

On both Laz32 / Win64 and Laz64 / Win64 I can extract jpg image.
On both Laz32 / Win64 and Laz64 / Win64 I get the generic pdf thumbnail when choosing a pdf.
On both Laz32 / Win64 and Laz64 / Win64 I get a SIGSEGV when I choose a LibreOffice document.

(The LibreOffice documents have correct thumbnails, with content, but no preview)

I need to find out why the LibreOffice documents give me a SIGSEGV.



Edit: Okay, this is a bummer:
Quote
We had to remove the thumbnail preview functionality from Acrobat and Reader DC for technical reasons. We know there are users who value it but certain design considerations forced us to make this change.
(In short, if you want PDF thumbnail previews, do NOT upgrade to Reader DC, because it breaks this thumbnail fix…)

Does anybody have thumbnails for new pdf's and Reader DC installed?

Edit #2:
Installing something like PDF Preview and setting it as default viewer I got my thumbnail back for PDFs. The 64-bit program you supplied works for me with Lazarus 64-bit on Windows 10 64-bit.
« Last Edit: August 28, 2015, 12:11:19 pm by rvk »

 

TinyPortal © 2005-2018