PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Open PDF in new window or tab.
https://forum.pdfsharp.net/viewtopic.php?f=2&t=353
Page 1 of 1

Author:  NateDawg [ Tue Apr 01, 2008 4:52 am ]
Post subject:  Open PDF in new window or tab.

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

Author:  Thomas Hoevel [ Tue Apr 01, 2008 7:45 am ]
Post subject: 

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.

Author:  NateDawg [ Wed Apr 02, 2008 8:10 pm ]
Post subject: 

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

Author:  NateDawg [ Thu Apr 03, 2008 5:23 pm ]
Post subject:  Got it.

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

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/