fedeteka
fedeteka

Reputation: 963

Force a webbrowser to display a PDF file only on Adobe Acrobat Reader

I create a PDF with iTextsharp and then I show the preview of the PDF inside a webbrowser control. From the preview the user can SAVE or PRINT using the defaults Adobe Reader's buttons

Working on Windows x64 bits with Adobe Reader as the default PDF viewer everything works fine.

The same program on a Windows x64 bits but with Foxit Reader as the default PDF open the file on Foxit Reader on full application window, outside my program.

I need to manage that.

My code is like

Dim PathToPDF As String
PathToPDF = DirectoryOfMyApp & "\ReportPreview.pdf"
ReportPreviewWebBrowser.Navigate(PathToPDF)

Where DirectoryOfMyApp just gets the C: or D: letter of the hard disk.

I read this link How to start an Adobe Reader or Acrobat from VB.NET?

but a line like

ReportPreviewWebBrowser.Navigate("acrobat", PathToPDF )

didn´t work and I think the webbrowser control don´t have the option to choose the PDF viewer

https://msdn.microsoft.com/es-es/library/system.windows.forms.webbrowser(v=vs.110).aspx

Is there a way to set the webbrowser to use Adobe Acrobat Reader only or to force any other PDF viewer to show the PDF inside the webbrowser control?

Upvotes: 1

Views: 4476

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415725

No, you can't do this.

You can't even guarantee that Adobe Reader is installed at all.

Reader might not even exist on the machine. It's not built into Windows, and not everyone uses it. Even if it is, FoxIt isn't the only alternative. A big one is that Chrome includes it's own PDF viewer.

Upvotes: 0

djangojazz
djangojazz

Reputation: 13242

I agree with Zaggler on his comments on this. You are making assumptions at a certain point on software that is installed on an end user's computer. Unless you are going to make the application's PDF viewer be part of a dependency installation or cooked into .NET you cannot guarantee they have that program to use. Nor can you guarantee it's installed location.

However there is a cheap hack for Windows based processes you can do in VB.NET. You can use the ole System.Diagnostics.Process()

Sub Main()
   Dim nProcess = New System.Diagnostics.Process()
   nProcess.Start($"D:\PdfFile.pdf")
End Sub

In this example I did a quick file location, you can try to ensure it is a valid location that will not change or is in your app's running process folder. This is really low tech as far as development goes, but it is basically saying: "Run me a process, any process, at this location. I don't care what it is, use the Windows defaults to determine what to do with it."

So when I run this on my Windows 10 Dev box it loads up Edge to display it, at home it would fire up Adobe Viewer. It is just opening the file essentially with the OS's choice of what is using that file extension. Not glamorous or very good for hardened code but it works when you want something quick to happen.

Upvotes: 2

Related Questions