0

I have an application that displays a pdf in a webBrowser control, using the following code

webBrowser1.Navigate(filename + "#toolbar=0");

It works perfectly if Adobe Reader is installed

I would like to check if Adobe Acrobat Reader is installed before displaying the window, or at least when trying to display the pdf.

I have adapted the following code from here Check Adobe Reader is installed (C#)?

As mentioned in the comments, unfortunately, it flags uninstalled versions as well. I have also tried the 64 bit code in the same article but find errors I can't easily resolve and suspect would give the same result anyway as it simmply looks at the registry in a similar way.

using System;
using Microsoft.Win32;

    namespace MyApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
                if(null == adobe)
                {
                    var policies = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Policies");
                    if (null == policies)
                        return;
                    adobe = policies.OpenSubKey("Adobe");
                }
                if (adobe != null)
                {
                    RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
                    if (acroRead != null)
                    {
                        string[] acroReadVersions = acroRead.GetSubKeyNames();
                        Console.WriteLine("The following version(s) of Acrobat Reader are installed: ");
                        foreach (string versionNumber in acroReadVersions)
                        {
                            Console.WriteLine(versionNumber);
                        }
                    }
                }
            }
        }
    }

If Adobe pdf Reader is not loaded, a prompt appears to open(in any other installed reader), save the file or cancel. I would like to be able to intercept this so as to Indicate that Adobe's reader is not available. I have tried

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            string url = e.Url.ToString();
            if (url.StartsWith("res://ieframe.dll/navcancl.htm") && url.EndsWith("pdf"))
            {
                e.Cancel = true;
                MessageBox.Show("Cannot open PDF!");
            }
        }

at the following https://social.msdn.microsoft.com/Forums/en-US/46aaeecd-5317-462a-ac36-9ebb30ba90e7/load-pdf-file-using-webbrowser-control-in-windows-form-c?forum=csharpgeneral but found the open, save cancel event precedes the webBrowser1_Navigating.

I would appreciate any help in a reliable solution that will not flag uninstalled versions, or a seperate solution that will stop the open, save, cancel prompt, and allow me to create a message to install the reader

Thanks

Big Vil
  • 7
  • 6

1 Answers1

0

The Simple Way is to use try / catch in the try give the code. The web browser will give error if there is an error so in the catch then say the user to install adobe reader. Here is the sample

try
{
//your code goes her
}
catch(Exception ex)
{
if(ex.Message != null)
{
Console.WriteLine("Adobe Reader not Installed");
//this is where you say adobe reader is not installed
}
}
Itachi Uchiha
  • 307
  • 4
  • 9
  • Unfortunately, not having Acrobat Reader installed does not throw an exception but simply goes to the Open/Save/Cancel options as described. Have tested to be sure, but this approach does not work. Thanks – Big Vil Jan 18 '21 at 10:20
  • Sorry For That Bro – Itachi Uchiha Jan 18 '21 at 10:53