Programmatically save PDF displayed inside Win Form

I am currious if it is possible to save a pdf that is being displayed in a web browser control or an adobe activex control inside my vb windows form. All I need it to do is pretty much save the pdf with the same name and location as it was opened, without any user intervention, so that I can stream it back into the database. I have seen alot of similar posts and I haven't really seen a solution to any of them.

Thanks in advanced
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can drop a WebBrowser control on a page and use the following code.
Imports System.Threading
Public Class Form1
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim u As String = e.Url.ToString()
        If (u.Substring(u.Length - 4, 4) = ".pdf") Then
            'new thread
            Dim ts As ThreadStart = New ThreadStart(AddressOf SaveFile)
            Dim t As Thread = New Thread(ts)
        End If
    End Sub
    Private Sub SaveFile()
        Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(WebBrowser1.Url.ToString())
        Dim response As System.Net.HttpWebResponse = request.GetResponse()
        Dim stream As System.IO.Stream = response.GetResponseStream()
        ' Get the length of the content
        Dim length As Integer = response.ContentLength
        ' Create a temporary array for the content of the file.
        Dim bytes(length) As Byte
        ' Get all bytes of the content and advance the progress bar.
        For i As Integer = 0 To length - 1
            bytes(i) = stream.ReadByte()
        ' Write the content to the local file.
        Dim filename As String = "c:\" & GetFileName()
        Using output As IO.Stream = System.IO.File.Create(filename)
            output.Write(bytes, 0, bytes.Length)
        End Using
    End Sub
    Private Function GetFileName() As String
        Dim u As String = WebBrowser1.Url.ToString()
        Dim lastIdxOf As String = u.LastIndexOf("/")
        Return u.Substring(lastIdxOf + 1, u.Length - lastIdxOf - 1)
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Url = New Uri("")
    End Sub
End Class

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Forced accept.

EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.