Results 1 to 19 of 19

Thread: [RESOLVED] Display PDF file in WebBrowser control

  1. #1

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Resolved [RESOLVED] Display PDF file in WebBrowser control

    I really hate asking all these questions, but my firewall blocks a lot of the links provided in examples on this forum. (Most forums)

    Can I use a Webbrowser control to preview a PDF document on a UserForm?
    I have seen post saying to use the WebBrowser control but didnt find enough info to create code.

    Any hints, tips or examples are appreciated.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Display PDF file in WebBrowser control

    Yes, provided that the user's computer have some pdf viewer addon for IE installed. If the user's computer have Adobe Reader installed then the addon is also installed.
    As for example, all you have to do is to add a webbrowser control to your form. When you want to open a specific pdf file, you call the Navigate method of the webbrowser and pass in the path to the pdf file.
    Code:
    dim pdffile as string = "path to pdf file here"
    WebBrowser1.Navigate(pdffile)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: Display PDF file in WebBrowser control

    I got it to work for the most part, now I just need help eliminating some things about the web control.
    Code:
        Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            Dim tmpStr As String
    
            sMyFileName = "F:\Rick\Drawings\" & ListBox1.Text
            tmpStr = "file:///" & sMyFileName
            WebBrowser1.Navigate(tmpStr)
        End Sub
    The control is a bit finicky on the string creation, as you can see I had to create a string for the file and path and then concat to another string for proper syntax.
    The text "file:///" is a required or it wont work. It has to become part of your entire file/path string.
    Example:
    "file:///C:\Folder\Filename.pdf" is the entire constructed string.

    I have attached a screen shot of my userform, I eliminated all the browser controls above the webBrowser1 control manually and that seems to be retained. Too bad I didnt document that part.

    Are there ways to format the PDF controls? Limit what is presented?
    Like PDF tabs on left and scroll bar on right?
    Attached Images Attached Images  
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  4. #4

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: Display PDF file in WebBrowser control

    Im also stuck on refreshing the browser view when the user selects a different file name from a listbox.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  5. #5

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: Display PDF file in WebBrowser control

    The complete project:
    ListBox1, WebBrowser1, TexBox1, Search Button for TextBox1 (Search in ListBox1), Process button and Cancel button.
    Oh, and Form1.
    Code:
    Option Explicit On
    Imports System.IO
    Imports System.IO.FileInfo
    
    Public Class Form1
        Dim sMyFileName As String
        Dim sMyString As String
        Dim tmpStr As String
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim fileNamesWithoutExtension() As String
    
            ListBox1.DataSource = New IO.DirectoryInfo("F:\Rick\Drawings\").GetFiles("*Bal.pdf", IO.SearchOption.TopDirectoryOnly)
            fileNamesWithoutExtension = Array.ConvertAll(ListBox1.Items.Cast(Of IO.FileInfo).ToArray, Function(fi) IO.Path.GetFileNameWithoutExtension(fi.FullName))
            ListBox1.SelectedIndex = -1
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Close()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If My.Computer.FileSystem.FileExists("F:\Rick\Drawings\" & ListBox1.Text) Then
                sMyFileName = "F:\Rick\Drawings\" & ListBox1.Text
                System.Diagnostics.Process.Start(sMyFileName)
            Else
                MsgBox(sMyFileName & " was not found.")
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            sMyString = TextBox1.Text
            Dim i As Integer
    
            For i = 0 To ListBox1.Items.Count - 1
                If ListBox1.GetItemText(ListBox1.Items(i)).Contains(sMyString) Then
                    ListBox1.SelectedIndex = i
                Else
                    MsgBox(sMyString & " was not found.")
                    TextBox1.Focus()
                    TextBox1.SelectAll()
                    Exit For
                End If
            Next
        End Sub
    
        Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            sMyFileName = "F:\Rick\Drawings\" & ListBox1.Text
            tmpStr = "file:///" & sMyFileName
            WebBrowser1.Navigate(tmpStr)
        End Sub
    End Class
    By adding a msgbox to the ListBox1 click event I see I read each file name, but the browser doesn't refresh, like the code after the msgbox is ignored.
    Code:
        Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            sMyFileName = "F:\Rick\Drawings\" & ListBox1.Text
            MsgBox(sMyFileName)
            tmpStr = "file:///" & sMyFileName
            WebBrowser1.Navigate(tmpStr)
        End Sub
    Last edited by sRLS; Feb 3rd, 2011 at 03:17 PM.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Display PDF file in WebBrowser control

    Works fine for me. Try this: 1 form, 1 listbox and 1 webbrowser.
    Code:
    Public Class Form2
    
        Dim table As DataTable
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.table = New DataTable
            With table.Columns
                .Add("Name", GetType(String))
                .Add("FullPath", GetType(String))
            End With
            'Load some data into the table
            Me.LoadRows("F:\")
            'Bind the table to listbox1
            Me.ListBox1.DisplayMember = "Name"
            Me.ListBox1.ValueMember = "FullPath"
            Me.ListBox1.DataSource = Me.table
        End Sub
    
        Private Sub LoadRows(ByVal dir As String)
            If IO.Directory.Exists(dir) Then
                Try
                    For Each fn As String In IO.Directory.GetFiles(dir, "*.pdf")
                        table.Rows.Add(IO.Path.GetFileNameWithoutExtension(fn), fn)
                    Next
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            End If
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            If Me.WebBrowser1.IsBusy Then
                Me.WebBrowser1.Stop()
            End If
            Dim pdf As String = Me.ListBox1.SelectedValue.ToString
            Me.WebBrowser1.Navigate(pdf)
        End Sub
    End Class
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: Display PDF file in WebBrowser control

    Same thing, I get a WebBrowser control refresh but the file displayed is the initial file I select.
    In my test folder there are 4 files to select from.

    I did a lot of testing and found that the first WebBrowser1.Navigate command I give it will not accept any others other than refresh.
    If I command this first:
    WebBrowser1.Navigate("") 'a blank page
    any subsequent navigation is ignored.

    If I command this:
    WebBrowser1.Navigate(tmpStr) ' first selected item
    then any other navigation is ignored.

    I used msgbox's before and after within the Click event of Listbox1 and can see the correct values are being passed, there just being ignored by the webbrowser control.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Display PDF file in WebBrowser control

    Did you actually try the code I posted? Start a new project and add 1 listbox + 1 webbrowser to the form and then use my code as is... Does it work?
    I have the feeling that your the file name is wrong because you append ListBox1.Text in the file name and ListBox1 displays file names without extension.
    Code:
     Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            sMyFileName = "F:\Rick\Drawings\" & ListBox1.Text
            MsgBox(sMyFileName)
            tmpStr = "file:///" & sMyFileName
            WebBrowser1.Navigate(tmpStr)
        End Sub
    Besides, no need to put "file:///" in front of your file path. Just pass in the full path to your pdf file to the Navigate method of the WB.
    Last edited by stanav; Feb 3rd, 2011 at 04:49 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Display PDF file in WebBrowser control

    Duplicate post removed...
    Last edited by stanav; Feb 3rd, 2011 at 04:47 PM. Reason: Accidental posted the same post twice
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: Display PDF file in WebBrowser control

    Yes, I definitely tried your code as you instructed.

    This has turned out to be a mistake on my part.

    I did not notice I had set "AllowNavigation" to "False" in the Properties window when I was wanting to set "AllowWebBrowserDrop" to "False".

    After setting "AllowNavigation" to "True" both of our individual code works fine.
    (Although yours is more, um, professional.)

    Thank you for your time and assistance!!

    Is it next year yet?
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Although the issue is resolved you still might like to look at the attached project.
    Attached Files Attached Files

  12. #12

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Im not sure if its due to converting at open or not but the viewer wont display any of my pdf files.
    The viewer is blank and there are no errors.

    If I add a msgbox to this section I assumed I would see the name of the file selected prior to open event.
    Code:
                 If OpenFileDialog.FileName <> FileName Then
                    '
                    '   If you know a specific page to navigate too replace 
                    '   'n' below with the page number
                    '       FileName = OpenFileDialog.FileName & "#page=n"
                    '   http://kb.adobe.com/selfservice/viewContent.do?externalId=317300
                    '
                    MsgBox(FileName)
                    wbDocument.Navigate(FileName)
                End If
    The msgbox is blank!?
    So....what'd I do wrong.

    p.s.
    I like the use of "#Region", "#End Region". Thats pretty handy.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Quote Originally Posted by sRLS View Post
    Im not sure if its due to converting at open or not but the viewer wont display any of my pdf files.
    The viewer is blank and there are no errors.

    If I add a msgbox to this section I assumed I would see the name of the file selected prior to open event.
    Code:
                 If OpenFileDialog.FileName <> FileName Then
                    '
                    '   If you know a specific page to navigate too replace 
                    '   'n' below with the page number
                    '       FileName = OpenFileDialog.FileName & "#page=n"
                    '   http://kb.adobe.com/selfservice/viewContent.do?externalId=317300
                    '
                    MsgBox(FileName)
                    wbDocument.Navigate(FileName)
                End If
    The msgbox is blank!?
    So....what'd I do wrong.

    p.s.
    I like the use of "#Region", "#End Region". Thats pretty handy.
    No idea why it fails for you. I tried this code on three machines, each with different versions of thePDF reader, two XP and one Vista box and all worked just fine.

    Try the attached VS2010 project which is the same as the VS2008 but fully done in Framework 4. When prompted to open a PDF select Demo.pdf (I created this specifically for this demo) first to see what happens then try other PDF files.
    Attached Files Attached Files

  14. #14

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: [RESOLVED] Display PDF file in WebBrowser control

    That one works fine. Didn't look at the guts yet, but I assure you I will.

    Must have something to do with the conversion process of 2008 to 2010 (my best guess, cause it can't be me )

    Thanks for this demo!
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Quote Originally Posted by sRLS View Post
    That one works fine. Didn't look at the guts yet, but I assure you I will.

    Must have something to do with the conversion process of 2008 to 2010 (my best guess, cause it can't be me )

    Thanks for this demo!
    Good to hear this worked for you.
    In regards to Regions, that are good but only when used in a prudent manner as done in the attached project where they are used not to hide code but to comment sections of code that stands out to someone examining the code.

  16. #16
    New Member
    Join Date
    Mar 2013
    Posts
    2

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Hi guys,

    Found your posts here and tried out the code. Works fine except for the fact that I get an error in the webbrowser and the pdf opens in Acrobat Reader in stead of in the webbrowser.
    Any idea on what I'm doing wrong ?

    Tnx in advance.

    Marc

  17. #17
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Quote Originally Posted by pretoriaan1971 View Post
    Hi guys,

    Found your posts here and tried out the code. Works fine except for the fact that I get an error in the webbrowser and the pdf opens in Acrobat Reader in stead of in the webbrowser.
    Any idea on what I'm doing wrong ?

    Tnx in advance.

    Marc
    That's because your Adobe Reader application has been set not to display pdf in a web browser. Try your application again after you've done this: open Adobe Reader program then go to Edit > Preferences. Select "Internet" under Categories and put a tick on "Display PDF in browser" checkbox then hit OK...
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  18. #18
    New Member
    Join Date
    Mar 2013
    Posts
    2

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Thanks for the quick response but it didn't work. The pdf still opens with Acrobat Reader and the webbrowser is showing the following message: Navigation to the webpage was cancelled. What you can try: Retype the address.

    Any other idea's?

    Grts,

    Marc

  19. #19
    New Member
    Join Date
    Apr 2013
    Posts
    1

    Re: [RESOLVED] Display PDF file in WebBrowser control

    Hi Marc, do you have any luck on that "opens with Acrobat Reader" bug? Im having the same trouble...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width