views:

781

answers:

2

I have a webbrowser object on a winform that I would like to use to display a pdf. The pdf resides on a ftp server. I have been able to show the pdf by downloading it to the disk and pointing the webbrowser object to it (navigate), but I want to stream it for security reasons. Has anyone been able to stream a pdf to a webbrowser that is located on a .Net winform?

    Dim URI As String = host & targetFilename
    Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

    ftp.Credentials = New System.Net.NetworkCredential(userName, passWord)
    ftp.KeepAlive = False
    ftp.UseBinary = True
    ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

    Using webResp As System.Net.FtpWebResponse = DirectCast(ftp.GetResponse(), System.Net.FtpWebResponse)
        Using respStream As Stream = webResp.GetResponseStream
            If GetFileExtension(targetFilename) = "PDF" Then
                WebBrowser1.DocumentStream = respStream
                Application.DoEvents()
            End If

            respStream.Close()
        End Using
    End Using
+1  A: 

I would recommend using a PDF Viewer Control rather than a web browser control, as a web browser control will require that the client have a PDF Viewer installed.

This will also allow you to stream a document to it.

Russ Bradberry
The boss doesn't like the idea of having to distribute more files to get this work. I will use a PDF Viewer Control if I can't get the webbrowser to work. Will the PDF Viewer Control work well with streamed data?
sparkkkey
Russ, I agree with you that the PDF Viewer Control would be the most "correct" way to do it. I just don't want to give up on the webbrowser yet.
sparkkkey
The PDF Viewer control will work with streamed data. It is also open source so there will only be the need of one other file to distribute. This is a much better alternative than getting calls from angry people who don't have Acrobat installed because their pdf viewer is showing a big red X
Russ Bradberry
I agree Russ. Thanks for helping me with this problem! I will have to put this project on hold until I can distribute the needed dependency.
sparkkkey
+1  A: 

Since you're already using the WebBrowser control; why not have it point to a local html file that includes an embed tag:

<embed src="ftp://ftpserver/yourpdf.pdf" />

I haven't tested it, but the pdf should be served within the context of the control.

Gurdas Nijor
Cool idea. Are you able to pass credentials ( username and password ) using a local html file?
sparkkkey
you can always pass credentials in plaintext, but it is not safe. the unsafe way to do this would be: ftp://user:password@ftpserver/yourpdf.pdf
Russ Bradberry
Thanks for the idea Gurdas! The security concerns (passing credentials) prevent me from implementing this soln.
sparkkkey