שתף באמצעות


scroll down a PDF file opened in webbrowser VB.net

Question

Friday, August 1, 2014 10:32 AM

i've opened a pdf file in webbrowser control , how could i make the pdf file scroll down.. 
i try the following but it give an error.. 
 
WebBrowser1.Document.Window.ScrollTo(New Point(0, 50)) 
 

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        WebBrowser1.Navigate("c:\d.pdf")

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        WebBrowser1.Document.Window.ScrollTo(0, 200)

    End Sub
    
End Class

All replies (12)

Friday, August 1, 2014 1:15 PM ✅Answered

If by chance you would like to scroll to a specific page instead of a arbitrary scroll check the following out. There is a function to get max pages so that when attempting to traverse to a page we know it's in range.

Place into a code module

Module Module1
    Public Function GetNoOfPagesPDF(ByVal FileName As String) As Integer
        Dim Result As Integer = 0
        Dim fs As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)
        Dim ReadStream As New IO.StreamReader(fs)
        Dim Contents As String = ReadStream.ReadToEnd()

        Dim regx As System.Text.RegularExpressions.Regex = _
           New System.Text.RegularExpressions.Regex("/Type\s*/Page[^s]")

        Dim matches As System.Text.RegularExpressions.MatchCollection =
            regx.Matches(Contents)

        Result = matches.Count

        Return Result

    End Function
End Module

Form has two buttons and one web browser control

''' <summary>
''' Shows how to open a PDF document to a specific page
''' with assertion
''' </summary>
''' <remarks></remarks>
Public Class Form1
    ''' <summary>
    ''' Change to a PDF that exists on your machine
    ''' </summary>
    ''' <remarks></remarks>
    Private FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sigma.pdf")
    ''' <summary>
    ''' Doomed to fail as there are only 24 pages
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim PageCount As Integer = GetNoOfPagesPDF(FileName)
        Dim intPageNumber As Integer = 400
        If intPageNumber <= PageCount Then
            WebBrowser1.Navigate("file:///" & FileName & "#page=" & intPageNumber)
        Else
            MessageBox.Show(
                String.Format(
                    "You specified page {0} but there are only {1} pages in this document",
                              intPageNumber, PageCount))
        End If
    End Sub
    ''' <summary>
    ''' This works as 8 is less than max pages of 24
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim PageCount As Integer = GetNoOfPagesPDF(FileName)
        Dim intPageNumber As Integer = 8
        If intPageNumber <= PageCount Then
            WebBrowser1.Navigate("file:///" & FileName & "#page=" & intPageNumber)
        Else
            MessageBox.Show(
                String.Format(
                    "You specified page {0} but there are only {1} pages in this document",
                              intPageNumber, PageCount))
        End If
    End Sub
End Class

 

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Friday, August 1, 2014 7:54 PM ✅Answered

I've only learned that the control was in my Choose Toolbox Items list under Com about a week or so ago. I guess it is because I have Adobe Acrobat Reader (AAR) installed on my system. I also have Adobe Flashplayer and Adobe Flashplayer Active X installed but don't believe either of those have anything to do with the AxAcroPDF control.

So if your system has AAR installed look in your Tools tab in Visual Studio and open Choose Toolbox Items and look in Com Components and it would be at the top if the list is in alpha numeric order.

Otherwise go to the AAR link above and download and install AAR on your system since it's free and then I suppose the control will be added to your Toolbox Items Com Components.

I checked it to add it to my Toolbox. Then went to my toolbox and dragged one onto my Form and clicked on it which created the last sub in the code below although I'm not sure what would be used inside of that sub.

Anyhow since the last image I posted I added a new line of code in the Form Load event which is the last line that causes the Toolbar and scroll bar to appear on the control when a .PDF file is loaded as in the 1st pic in the image below. The 2nd pic shows my Choose Toolbox Items menu display the Com Components.

Also if I don't set compile to x86 CPU rather than Any CPU or x64 CPU my app will crash as an error occurs with this control. Don't know if that is a problem with my system or a requirement for this control or if something on my system is misconfigured.

So I don't really know anything about the control. MSDN does not have any documentation on it. I haven't looked at Adobes website to see if they have developers documentation on the control.

EDIT: I just noticed that if I set the "View size"? from 59.4% to 100% and the .PDF window became wider than the area it is contained in then the horizontal scrollbar shows up also.

Code

Option Strict On

' Has to be compiled to x86 CPU on my system or app crashes.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        AxAcroPDF1.setShowToolbar(True) ' Displays toolbar at top of control and scrollbar on right of control.
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim OFD As New OpenFileDialog
        OFD.Title = "Load .PDF file"
        OFD.InitialDirectory = "C:\Users\John\Desktop"
        OFD.Multiselect = False
        OFD.Filter = "PDF files (*.PDF)|*.PDF"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            AxAcroPDF1.LoadFile(OFD.FileName)
            AxAcroPDF1.setCurrentPage(10) ' Sets displayed .PDF to page 10. May crash if .PDF doesn't have 10 pages though for all I know.
        End If
    End Sub

    Private Sub AxAcroPDF1_OnError(sender As Object, e As EventArgs) Handles AxAcroPDF1.OnError

    End Sub

End Class

Image

La vida loca


Friday, August 1, 2014 10:57 AM

i've opened a pdf file in webbrowser control , how could i make the pdf file scroll down.. 
i try the following but it give an error.. 
 
WebBrowser1.Document.Window.ScrollTo(New Point(0, 50)) 
 

That code is correct with the exception of using a point. Instead use integers.

WebBrowser1.Document.Window.ScrollTo(200, 200) or whatever position.

You could even use two numeric up down controls to do it.

WebBrowser1.Document.Window.ScrollTo(Cint(NumericUpDown1.Value), Cint(NumericUpDown2.Value))

Also you may want to see the below thread for hiding the scrollbars in the webbrowser control. Plus some optical zoom stuff for use with the webbrowser control.

VB.Net "webBrowser1.ScrollBarsEnabled = false' does not work in FEATURE_BROWSER_EMULATION ie9

La vida loca


Friday, August 1, 2014 12:36 PM

thank you for your reply , 
i've try the code that you write , but it give me the same error ( Object reference not set to an instance of an object.)

when i try to open a web Page in the web browser , the code is work as charm . 

but the PDF file is located in my computer , perhaps that's the problem. 

any other idea ?


Friday, August 1, 2014 12:47 PM

thank you for your reply , 
i've try the code that you write , but it give me the same error ( Object reference not set to an instance of an object.)

when i try to open a web Page in the web browser , the code is work as charm . 

but the PDF file is located in my computer , perhaps that's the problem. 

any other idea ?

Without seeing your code it's hard to tell what area of it may be causing that error.

Which line of your code shows this error? Can you display all of your code and highlight this line in it?

If so use the Insert Code Block function. It's in the toolbar at the top of an open post, the little square button to the right of the letters HTML. Copy and paste your code into the code block, preview it and insert it into your post. It will be inserted wherever your caret was in the post at the time that you clicked on that button.

Then after you add the code block to your post scroll down to that line, highlight it with the mouse and select the B letter in the toolbar to make that line bold perhaps.

La vida loca


Friday, August 1, 2014 1:28 PM

i've insert the Code , Please check it 
the code is so simple, a webbrowser navigate to a pdf file , and a button to scroll the file down once it's clicked .

once i click the button , i got the error( Object reference not set to an instance of an object.)

on the Bold line


Friday, August 1, 2014 1:57 PM

Thank you , i think this could be an answer , i want to scroll to a specific location , but this could also be an option.. thank you 


Friday, August 1, 2014 2:08 PM

thank you for your reply , 
i've try the code that you write , but it give me the same error ( Object reference not set to an instance of an object.)

when i try to open a web Page in the web browser , the code is work as charm . 

but the PDF file is located in my computer , perhaps that's the problem. 

any other idea ?

After navigating to the PDF document in the web browser add the following code in a button click event

Dim test As HtmlDocument = WebBrowser1.Document
Debugger.Break()

Hover over test and you will see the test is null/nothing which is because the Document is never set because you have not set the Document but instead used Navigate. With that said there is no method to scroll in a conventional manner which is why I made the suggestion to use the code I suggested to scroll by page.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


Friday, August 1, 2014 3:22 PM

i've insert the Code , Please check it 
the code is so simple, a webbrowser navigate to a pdf file , and a button to scroll the file down once it's clicked .

once i click the button , i got the error( Object reference not set to an instance of an object.)

on the Bold line

Is there a reason you want to use the WebBrowser control to view .PDF files on your PC rather than using the Adobe Reader control which I believe becomes available if you download and install Adobe Reader, which is free, from Adobes website?

An image of it is below on a Form. I've only used it once and haven't set it for showing controls or other things yet and don't know its entire display capability.

La vida loca


Friday, August 1, 2014 6:56 PM

Is there a reason you want to use the WebBrowser control to view .PDF files on your PC rather than using the Adobe Reader control which I believe becomes available if you download and install Adobe Reader, which is free, from Adobes website?

An image of it is below on a Form. I've only used it once and haven't set it for showing controls or other things yet and don't know its entire display capability.

no there's no reason , i only want to display the pdf file inside the windows form , i wasn't know about this method in Adobe Reader .
could i Scroll the file as i like in this method ? 
i'll give it a try , thank you for your help 


Friday, August 1, 2014 9:24 PM

thank you for the info and the great effort , appreciate it 


Friday, August 1, 2014 9:26 PM

yes i see the problem now , appreciate your help kevininstructor , thank you