views:

164

answers:

3

Hi, i am using c# to create a button for IE and this button performs certain actions that all depend on the document being a PDF document. I am trying to setup a guard to prevent any action taking place if the document type is not a PDF but not sure how as IE hands over the document to adobe and reader takes charge. I am using both SHDocWv have looked at the WebBrowserClass objects and not sure how to figure this out. any suggestions?

A: 

Check mime type of the document or see the window.location.href of webbrowser... If pdf is being displayed, you would be able to find it...

Software Enthusiastic
unable to check URL as some sites return query strings without "pdf" in the address. can you tell me how to access the mime types and from which class / object?
Grant
+1  A: 

It's a little bit problematic to do this AFAIK.

Value of IWebBrowser2::Type property depends on what plug-in you have installed that handles PDFs, because some plug-ins creates HTML wrapper for PDF file (like Adobe) so you get "HTML Document" as type and some plug-ins don't do this (like Foxit), so you can't relay on this exclusively.

So if you got PDF with HTML wrapper you can use IHTMLDocument2::mimeType to find out exact type of the document (JPEG/GIF/PNG/etc. files are all wrapped in HTML by the browser). But as I know it is unreliable too, for instance on my machine it returns "Firefox Document" for HTML documents because .html files are associated with Firefox :s But I didn't test to see if this is the case with PDFs alos.

Another options is to use GetUrlCacheEntryInfoEx API call to obtain file in local browser cache which stores document, then read it (only the beginning of the file, I think only the first 256 bytes are important) and call FindMimeFromData with data you just read and it will return mime type.

Mladen Jankovic
A: 

Another good way is to do the following..

1] Cast the Document object to IPersist and then extract the CLSID using .GetClassID(..). 2] pInvoke ProgIDFromCLSID to extract the progId 3] Match the progID against known COM objects / applications.

Grant