PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Open PDF in new window or tab.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
NateDawg



Joined: 01 Apr 2008
Posts: 6

PostPosted: Tue Apr 01, 2008 4:52 am    Post subject: Open PDF in new window or tab. Reply with quote

On my site I would like to give users the option to:
1) Open PDF in the same window.
2) Open a Save file dialog box.
3) Open PDF in a new window/tab (popup).

I've figured out how to do one.

Dim stream As MemoryStream = New MemoryStream
document.Save(stream, False)
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Accept-Header", stream.Length.ToString)
Response.AddHeader("content-length", stream.Length.ToString)
Response.OutputStream.Write(stream.GetBuffer(), 0, stream.Length)
Response.Flush()
Response.End()
stream.Close()

Then to do two I just add the following line before I output the stream.
Response.AddHeader("content-disposition", "attachment; filename=Invoice.pdf")

But I'm stuck on my third option, and I'm just certain the client is going to want that feature more than the other two. Does anyone know how to push the output stream to a popup window?

Thanks, Nathan Rover
Back to top
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Tue Apr 01, 2008 7:45 am    Post subject: Reply with quote

Have you tried 'target="_blank"' at the A tag that opens the PDF file?
AFAIK you have to deal with client-side code (Javascript) to open the PDF file in a popup window.
_________________
Regards
Thomas Hoevel
PDFsharp Team
Back to top
View user's profile Send private message Visit poster's website
NateDawg



Joined: 01 Apr 2008
Posts: 6

PostPosted: Wed Apr 02, 2008 8:10 pm    Post subject: Reply with quote

Ok, getting a little closer to the goal, if IE7 didn’t exist I’d be done…

Here’s what I’ve got going so far.

On my page that is to launch the PDF I have an imagebutton. When that button is clicked the post back runs a program to collect some data.
It then saves the data to state like so:

Code:
Dim SID As New ArrayList
Session.Clear()
Session.Add("InvoiceSID", SID)
SID.Add(StudentID)
Session("InvoiceSID") = SID


Then if the user preference is to open a new tab it runs this code.
Code:

If Page.FindControl("HiddenURL") Is Nothing Then
    Page.ClientScript.RegisterHiddenField("HiddenURL", "URL to PDFInvoice")
Else
    CType(Page.FindControl("HiddenURL"), HiddenField).Value = "URL to PDFInvoice"
End If

Dim myScript As String = "if (document.getElementById(""HiddenURL"").value !=
""""){window.open(document.getElementById(""HiddenURL"").value, ""_blank"");
document.getElementById(""HiddenURL"").value = """";}"

Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "ScriptFunction", myScript, True)

If the user preference is to open in same window it runs this code.
Code:

Response.Redirect("URL To PDFInvoice")

On my page that is to “catch” this request and build the PDF I have this code in the ASCX
Code:

<h1>ERROR Launching PDF!</h1>

Ooo-ahh, I know… It’s just there in case something goes wrong. In the ASCX.VB I have this code:
Code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
        Dim SID As New ArrayList
        SID = Session("InvoiceSID")
        'MakeInvoice(SID) 'MakeInvoice() is >300 lines of code
        MakeTest()        'for this example I’ll use MakeTest()
    End If
End Sub

Private Sub MakeTest()
    ' Create a new PDF document
    Dim document As PdfDocument = New PdfDocument

    ' Create an empty page
    Dim page As PdfPage = document.AddPage
    page.Orientation = PageOrientation.Landscape
    'Create fonts
    Dim Helvetica10 As XFont = New XFont("Helvetica", 10, XFontStyle.Regular)

    ' Get an XGraphics object for drawing
    Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
    Dim L As Double = 2.0

    'Put string
    gfx.DrawString("Hello?", Helvetica10, XBrushes.Black, L, 72)

    ' Send Document to the browser
    Dim stream As MemoryStream = New MemoryStream
    document.Save(stream, False)
    Response.Clear()
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-length", stream.Length.ToString)
    Response.BinaryWrite(stream.ToArray)
    Response.Flush()
    stream.Close()
    Response.End()
End Sub

All that said… it all works perfect in Firefox and Safari. In IE7 only the option to open in the same window works. If you try and open in a new tab IE7 will open the tab and shows a blank screen.

Here is the code view.
Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

The kicker is, If I comment out” MakeTest()” I get my “ERROR launching PDF!” message. So, it must be trying to make the PDF but for some reason it's braking. Moreover, why is it only breaking when I open in new tab? It’s the same code that runs when you open it in the same and in a new tab.

I’m at a loss as to what I should do next do you have any thoughts or suggestions?

Thanks, Nathan Rover
Back to top
View user's profile Send private message
NateDawg



Joined: 01 Apr 2008
Posts: 6

PostPosted: Thu Apr 03, 2008 5:23 pm    Post subject: Got it. Reply with quote

Well, I figured it out, with a little help:


http://forums.asp.net/p/1036628/1436084.aspx


The problem was in how the document was being sent to the browser. Here is the code that works.

Code:

Dim stream As MemoryStream = New MemoryStream
document.Save(stream)
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "inline; filename=Report.pdf")
Response.BinaryWrite(stream.ToArray)
Response.End()


I’m still not too sure why it works but I think IE7 was dropping the stream too soon. This isn’t really a problem with PDFsharp but I figured this would be useful information for anyone using PDFsharp to output to the browser like I am. This is tested to work in IE7, Firefox 2, Opera 9 and Safari 3.

-- Nathan Rover
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com