How to Display a PDF in an ASP.Net Application

aspnet.pngIt should be easier than it is.

In Q30654, Microsoft suggest the following:

private void Page_Load(object sender, System.EventArgs e)
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";

//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.

The only thing is, for some versions of Acrobat Reader, this doesn’t actually work. On my desktop PC (IE 6.0.2800.1106 with Acrbat Reader this results in a blank page.

For some wierd reason, the PDF is displayed if it is rendered into the page during a post-back. When I put a button on the page and included the code above in the click event, it displayed perfectly well.

To work-around the issue, I now post a (virtually) blank page to the user’s browser. On the page there is a script to post the page back. Then I render the PDF in the postback. Not exactly elegant, but it works.

Here is my code (VB.Net-ed because that is the language favoured by my employer):

''' <summary>
''' Handles the Load event of the Page control.
''' </summary>
''' <param name="sender">The source of the event.</param>
''' <param name="e">The <see cref="T:System.EventArgs" /> instance containing the event data.</param>
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
' HACK: There is a bug in some versions of
' AcrobatReader that prevent it from rendering a
' PDF during an initial page load - it will only
' render on postback. As a work-around, we generate
' a page that does nothing except post itself back.
' We can then render the report as a PDF in the
' postback. Unfortunately, the browser "refresh"
' still doesn't work very well.

' If this is not a postback...
If Not IsPostBack Then
' Pretend that there is a legitimate reason for
' the spurious round-trip to the client:
' display something informative.
Response.Write("Your report is loading...")
' Generate a script that will post-back the
' page.
Dim script As String = String.Empty
script = "<script language=javascript>" & Page.ClientScript.GetPostBackEventReference(Me, "") & "</script>"
' Inject the postback script into the page.
' When the page is rendered, it will post back
' immediately.
ClientScript.RegisterStartupScript(GetType(Page), "PostIt", script)
' This is a postback. Render the report.
End If
End Sub
''' <summary>
''' Shows the report.
''' </summary>
Private Sub ShowReport()
' Set the appropriate ContentType.
Response.ContentType = "Application/pdf"
' Get the physical path to the file.
Dim FilePath As String = MapPath("acrobat.pdf")
' Write the file directly to the HTTP content
' output stream.
End Sub

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: