Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Launching PDF without saving it first

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-11 : 11:54:32
I need to launch a pdf document from vb.net without forcing user to save the document first. I want the user to just be able to view it.

Is this possible?

Thanks,
Ninel

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-04-11 : 14:51:27
Yep!
VB.net or ASP.net??
In ASP.Net what you need to do is on the server side, load the file into a Stream (MemoryStream I think is best), do a Response.Clear, then do a Response.Contenttype = "application/pdf", then do a Response.BinaryWrite of the stream. Be sure to close your stream!

Hope that helps, enough though that was a serious run-on sentance!

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-11 : 14:57:33
Thank you, but I need it in vb.net.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-04-11 : 17:35:18
Ok, walk me through what you are doing. I may be able to help.
I'm guessing you a generating a PDF somehow, storing it in memory, and then you want to "open" that in-memory file with Acrobat reader?

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-13 : 12:18:19
I have a table of pdf files that need to be displayed to an employee. When an employee punches in I check if they viewed the pdf files. If not, I show a link on the login page for them to click on. In order to track the clicking of that link I use the .navigateurl to go to another page where I can update a table. I then want to display the pdf.

I am doing the following:
To test initially, I saved one pdf in the same directory as the login.aspx page of my .net project.

Here's the code:

Login.aspx :
hlnkTest.Visible = True
hlnkTest.Target = "_blank"
hlnkTest.Text = "TEST"
hlnkTest.NavigateUrl = "CaptureClickandDisplayPDF.aspx"

CaptureClickandDisplayPDF.aspx :

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim FilePath As String

Response.ContentType = "Application/pdf"
FilePath = MapPath("TestPDF.pdf")
Response.WriteFile(FilePath)
End If
End Sub


I don't get an error message, but get mumbo jumbo displayed and not the actual contents of the pdf file.

What am I doing wrong? Is there another way to do this?

Thanks,
Ninel
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-04-13 : 14:57:51
I've not had do to this in ASP.Net yet, but from the looks of things, that's pretty close.

I think you need to do this:

Response.ContentType = "application/pdf" 'capital A there may be an issue, notu sure

FilePath = MapPath("TestPDF.pdf") 'This may need to be Server.MapPath and not just MapPath. They return two different things

response.clear
Response.WriteFile(FilePath)
response.flush


Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-13 : 15:07:58
Nope. That didn't work. I was getting a blank page.

I did it this way...

Response.ContentType = "Application/pdf"
Response.Redirect("05. Incentive Policy.pdf")


I saved the pdf in the sasme directory as the aspx page in my .net project and it worked.
Go to Top of Page

jubinjose
Starting Member

20 Posts

Posted - 2006-04-14 : 05:12:08
It didn't work first time prbly coz aspnet suer account has no access to the folder in which pdf was saved. Try giving permission to aspnet user on the folder and see if you get it
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-04-14 : 11:38:45
Turns out my manager doesn't want me to do it this way because I would have to save the pdf files in my .net project.

In addition, there will be a checkbox on the first page that is disabled until the link is clicked. Once the link is clicked I need to launch another window for the pdf file and at the same time enable the checkbox and update the database to track that the employee checked the checkbox.

Can I do something like this...
User clicks on link on first page and sets off a javascript function on the onclick event that enables the checkbox and launches the 2nd window.

I have the following code:

Page1.aspx
HTML:
<asp:hyperlink id="hlnkTest" runat="server" Font-Names="Tahoma" Font-Size="x-Small" ForeColor="BLUE" Visible="true"></asp:hyperlink>
<asp:checkbox id="chkRead" runat="server" OnCheckedChanged="chkRead_checked" AutoPostBack="true" Font-Size="x-Small" Font-Names="Tahoma" Text="I have read the new/updated policies"></asp:checkbox>

ASPX:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
hlnkTest.Visible = True
hlnkTest.Target = "_blank"
hlnkTest.Text = "TEST"
hlnkTest.Attributes.Add("onclick", "enableCheckBox()")
hlnkTest.NavigateUrl = "Page2.aspx"
chkRead.Enabled = False

Dim str As String
str = "<script language='javascript'>"
str = str & "function enableCheckBox() {document.getElementById('chkRead').disabled = false;}"
str = str & "</script>"
Response.Write(str)
End If
End Sub


First...I'm not getting an error, but the checkbox is getting enabled either.
Second..How can I call a function that updates the database when user checks the checkbox using this code?

Thanks,
Ninel
Go to Top of Page
   

- Advertisement -