dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Saving PDF on local hard disk which is read by browser control

  1. #1
    Join Date
    Aug 2007
    Posts
    20

    Question Saving PDF on local hard disk which is read by browser control

    Hi,

    I am writing program VB.Net (using Visual studio 2005). In this program, I am using browser control to send request on the internet. In responce to the request, one PDF file gets opened in the browser automatically. I want to save this PDF file on the hard disk. To do this, I think my program should be able to read the downloaded file in response to my request. Is there any way by which I can do this.

    Thanks in advance.

    Regards,
    Sunil

  2. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Saving PDF on local hard disk which is read by browser control

    Quote Originally Posted by sunil_shindekar
    ...
    In responce to the request, one PDF file gets opened in the browser automatically. I want to save this PDF file on the hard disk. To do this, I think my program should be able to read the downloaded file in response to my request. Is there any way by which I can do this.
    ...
    If you know the link of the PDF, maybe it would be easier to download it as a file, and then open it in the browser.

  3. #3
    Join Date
    Aug 2007
    Posts
    20

    Re: Saving PDF on local hard disk which is read by browser control

    My interest is not opening it in a browser. My interest is in saving on the hard disk. However I can not access it directly giving any specific URL. The flow is is as follows.

    1) My application open a web page in WebBrowser control after sending some URL , say www.xx.com (using Navigate method)
    2) The opened web page(www.xx.com) has fields like Name, Address, SSN. My application fills this information in the browser window programatically and clicks on one link in the same web page (this is also programatically)
    3) on clicking the link, the filled infomration gets submitted and at the server end(of www.xx.com) it processes entered name, address and SSN and generates one PDF file. This PDF file gets opened in my browser(webbrowser) window.
    4) This opened PDF, I want to save on the hard disk programatically without having to click manually on the save icon of the embedded acrobat reader.

    I am unable to achieve step 4.

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Saving PDF on local hard disk which is read by browser control

    There are a couple of ways i can see to do this.

    1. Find the window handle and location of the browser and send messages to save the pdf file. This might be a bit tough as you will need to send keys and clicks in a particular order to get it to work right.
    2. Save a copy of all the pages from XX.com and write your own small browser for this application. It is not really to tough, and there are examples here of how to craft an HTTP header and request. Doing it this way, the PDF file will be sent directly to your program so you can save it where ever you want.

  5. #5
    Join Date
    Aug 2007
    Posts
    20

    Re: Saving PDF on local hard disk which is read by browser control

    Quote Originally Posted by sotoasty
    There are a couple of ways i can see to do this.

    1. Find the window handle and location of the browser and send messages to save the pdf file. This might be a bit tough as you will need to send keys and clicks in a particular order to get it to work right.
    2. Save a copy of all the pages from XX.com and write your own small browser for this application. It is not really to tough, and there are examples here of how to craft an HTTP header and request. Doing it this way, the PDF file will be sent directly to your program so you can save it where ever you want.

    First method can be used. But I wish to avoid it if I get better method like directly reading or saving the PDF. It is not only because if it difficult. It is because I feel it is not very elegant method.

    About the second method, if we know how the PDF is received by the browser, then probably we can get it even using WebBrowser control. However I currently don;t know how PDF is received by by the browser. I tried getting the acrobat activex control from webBrowser. However it doesn't have Save method.
    Last edited by sunil_shindekar; May 30th, 2008 at 02:59 AM. Reason: typing mistake

  6. #6
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Saving PDF on local hard disk which is read by browser control

    Quote Originally Posted by sunil_shindekar
    ...I currently don;t know how PDF is received by by the browser. I tried getting the acrobat activex control from webBrowser. However it doesn't have Save method.
    It would help to know whay libraries/references are you using.
    I created a new application, and added a reference to "Adobe Acrobat 7.0 Browser Control Library 1.0" (AcroPDF.dll), navigated it with the VB Object Browser, and found a .Save function:
    Function Save(ByVal nType As Short, ByVal sFullPath As String) As Boolean
    Member of Acrobat.AcroPDDoc.CAcroPDDoc

    I suggest you to investigate it.
    _

  7. #7
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Saving PDF on local hard disk which is read by browser control

    Its probably not as difficult as you think..

    You're taking a long and hard approach to this...

    Think of it like this, you send a request to the webserver and it sends you back a response. You will be posting the answers to your form in your request and getting back a pdf file.

    Give this code a go (obviously changing the URL and strPostData)
    Code:
    Imports System.IO
    Imports System.Net
    Imports System.Text
    
    Class RetrievePDF
        Public Shared Sub Main()
    
            Dim URL As String = "http://www.nikiteforum.com/pdfdownload.aspx"
            Dim strName As String = "Mike"
            Dim strAddress As String = "SomeAddress"
            Dim strSSN As String = "Dunno"
    
            Dim strPostData As String = String.Format("Name={0}&Address={1}&SSN={2}", strName, strAddress, strSSN)
    
            Dim wr As HttpWebRequest = WebRequest.Create(URL)
            wr.Method = "POST"
            wr.ContentLength = strPostData.Length
            wr.ContentType = "application/x-www-form-urlencoded"
            wr.CookieContainer = New CookieContainer
    
            Dim sw As StreamWriter = New StreamWriter(wr.GetRequestStream)
            sw.Write(strPostData)
            sw.Close()
    
            Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
            Dim str As Stream = ws.GetResponseStream()
            Dim inBuf(100000) As Byte
            Dim bytesToRead As Integer = CInt(inBuf.Length)
            Dim bytesRead As Integer = 0
            While bytesToRead > 0
                Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
                If n = 0 Then
                    Exit While
                End If
                bytesRead += n
                bytesToRead -= n
            End While
            Dim fstr As New FileStream("download.pdf", FileMode.OpenOrCreate, FileAccess.Write)
            fstr.Write(inBuf, 0, bytesRead)
            str.Close()
            fstr.Close()
        End Sub
    End Class
    GL
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  8. #8
    Join Date
    Aug 2007
    Posts
    20

    Re: Saving PDF on local hard disk which is read by browser control

    I will try this. But will this work on for the site which is secured using SSL. My first impression is that it will not work as the values are not encrypted.

    Quote Originally Posted by HairyMonkeyMan
    Its probably not as difficult as you think..

    You're taking a long and hard approach to this...

    Think of it like this, you send a request to the webserver and it sends you back a response. You will be posting the answers to your form in your request and getting back a pdf file.

    Give this code a go (obviously changing the URL and strPostData)
    Code:
    Imports System.IO
    Imports System.Net
    Imports System.Text
    
    Class RetrievePDF
        Public Shared Sub Main()
    
            Dim URL As String = "http://www.nikiteforum.com/pdfdownload.aspx"
            Dim strName As String = "Mike"
            Dim strAddress As String = "SomeAddress"
            Dim strSSN As String = "Dunno"
    
            Dim strPostData As String = String.Format("Name={0}&Address={1}&SSN={2}", strName, strAddress, strSSN)
    
            Dim wr As HttpWebRequest = WebRequest.Create(URL)
            wr.Method = "POST"
            wr.ContentLength = strPostData.Length
            wr.ContentType = "application/x-www-form-urlencoded"
            wr.CookieContainer = New CookieContainer
    
            Dim sw As StreamWriter = New StreamWriter(wr.GetRequestStream)
            sw.Write(strPostData)
            sw.Close()
    
            Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
            Dim str As Stream = ws.GetResponseStream()
            Dim inBuf(100000) As Byte
            Dim bytesToRead As Integer = CInt(inBuf.Length)
            Dim bytesRead As Integer = 0
            While bytesToRead > 0
                Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
                If n = 0 Then
                    Exit While
                End If
                bytesRead += n
                bytesToRead -= n
            End While
            Dim fstr As New FileStream("download.pdf", FileMode.OpenOrCreate, FileAccess.Write)
            fstr.Write(inBuf, 0, bytesRead)
            str.Close()
            fstr.Close()
        End Sub
    End Class
    GL

  9. #9
    Join Date
    Aug 2007
    Posts
    20

    Re: Saving PDF on local hard disk which is read by browser control

    I tried on Acrobat 7.0 as well as 8.0 after adding the reference of acropdf. But I could not find the class mentioned by you. Is it part of writer? I don;t have the writer. I have only reader which is freeware.

    Quote Originally Posted by Marraco
    It would help to know whay libraries/references are you using.
    I created a new application, and added a reference to "Adobe Acrobat 7.0 Browser Control Library 1.0" (AcroPDF.dll), navigated it with the VB Object Browser, and found a .Save function:
    Function Save(ByVal nType As Short, ByVal sFullPath As String) As Boolean
    Member of Acrobat.AcroPDDoc.CAcroPDDoc

    I suggest you to investigate it.
    _

  10. #10
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Saving PDF on local hard disk which is read by browser control

    Quote Originally Posted by sunil_shindekar
    I tried on Acrobat 7.0 as well as 8.0 after adding the reference of acropdf. But I could not find the class mentioned by you. Is it part of writer? I don;t have the writer. I have only reader which is freeware.
    I have only installed the acrobat reader from here.
    On your project, after click on Project menu>Add Reference...>COM tab, you must see the Adobe Acrobat x.xx Browser Control Type Library, and Adobe Acrobat XX type library

    Once you click on both, you must have acces to:
    Public Overridable Function Save(ByVal nType As Short, ByVal sFullPath As String) As Boolean
    Member of Acrobat.AcroPDDocClass

    and

    Function Save(ByVal nType As Short, ByVal sFullPath As String) As Boolean
    Member of Acrobat.CAcroPDDoc

  11. #11
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Saving PDF on local hard disk which is read by browser control

    Quote Originally Posted by sunil_shindekar
    I tried on Acrobat 7.0 as well as 8.0 after adding the reference of acropdf. But I could not find the class mentioned by you. Is it part of writer? I don;t have the writer. I have only reader which is freeware.
    Is really Simple!!

    Code:
    	Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
    
    		WebBrowser1.ShowSaveAsDialog()
    	End Sub
    
    	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    		WebBrowser1.Navigate("http://pb.rcpsych.org/cgi/reprint/3/4/68.pdf")
    
    	End Sub
    Is not even necessary to add references to Acrobat libraries.

    (Found here http://forums.msdn.microsoft.com/en-...-c57f46269588/)
    _

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)