Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Launch PDF File From ASP.Net VB Code-Behind

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
LAY OF THE LAND: I am generating PDF files on the fly using Crystal Reports. I am saving these files on my server's C: drive in a directory called MyPDFs (i.e., C:\MyPDFs). The security on this directory allows full control to only administrators and the ASPNET user account (trying to prevent web users from directly accessing the files). I have a Win2K server with IIS 5.0 and I am using ASP.Net.

QUESTION: In the VB code behind my ASP.Net Web Form, I want to launch a PDF file located in the above mentioned directory ("C:\MyPDFs"). My VB code-behind assembles the proper PDF filename depending on what the user selects on the Web Form; however, for purposes of this question, assume the filename is MyFile.pdf ("C:\MyPDFs\MyFile.pdf"). What VB code do I use to launch this PDF file for the Web User? Game Over, Man!
 
I have managed to get the rename a pdf through an ASP.NET app, and display a link to the PDF using this code running on local host:

Imports Acrobat

Dim gApp As CAcroApp
Dim gPDDoc As CAcroPDDoc
Dim myDoc As CAcroPDDoc


Try
gApp = CreateObject("AcroExch.App")
gPDDoc = CreateObject("AcroExch.PDDoc")

gPDDoc.Open("C:\Bob.pdf")

gPDDoc.Save(1, "C:\bob2.pdf")

gPDDoc.Close()

Response.Write
(&quot;<a href='file:///C:/bob2.pdf'>Downlaod</a>&quot;)
Catch ex As Exception
Throw ex
Finally
myDoc = Nothing
gApp = Nothing
End Try
 
You can stream the file into your page. I use C#, so forgive me not knowing the VB.NET syntax, but you use the Response object.

Response.ContentType = &quot;application/pdf&quot;;
Response.WriteFile(C:\MyPDFs);
Response.Flush();

This writes the PDF directly into the browser, so you don't have to worry about rights to the directory. Your app reads the file into memory, and streams it out. That way the browser isn't trying to browse to a secure directory, it just receives the PDF as a stream of bytes from your ASP.NET app.

Thomas D. Greer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top