How to Load a PDF File to a Web Browser in ASP.NET Using VB.NET

If you are an ASP.NET developer using VB.NET to code Web pages, you have the ability to display PDF documents using very little code. Those PDFs can reside on your Web server or on any other server on the Internet. Giving site visitors the opportunity to view PDFs in their Web browsers saves time because the users don't have to download the files first. All you need to test this built-in ASP.NET functionality is a VB.NET Web page and the name of a PDF you want to display in your browser.

Instructions

    • 1

      Launch Visual Studio and open one of your ASP.NET projects that uses VB.NET.

    • 2

      Press "Ctrl," "Shift" and "A" at the same time to display the "Add New Item" window. Click "Web Form" and then type "Test_PDF_Load.aspx" in the Name text box. Click "Add" to add the new form to the project.

    • 3

      Click the "Design" button at the bottom of Visual Studio to view the form in design mode.

    • 4

      Press "Ctrl," "Alt" and "X" at the same time to open the "Toolbox" window. Double-click the "Button" control inside the toolbox to place it on the form.

    • 5

      Double-click the button you placed on the form to view the button's "Click" event code. This code looks like this:

      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

      End Sub

      End Class

    • 6

      Replace that code with the code shown below:

      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim pathToPDF As String

      pathToPDF = "PDFNAME"

      Response.Redirect(pathToPDF)

      End Sub

      End Class

      Replace "PDF_NAME" with the name of a PDF file on your Web server or the URL of any PDF file on the Web.

    • 7

      Locate the "Test_PDF_Load.aspx" file in the "Solution Explorer" window and right-click that file. A menu opens. Click "Set as Start Page" and then press "F5" to run the project. Your browser displays the ASP.NET page with the button you added.

    • 8

      Click the button. It runs the "Click" event code and executes the "Response.Redirect" statement that loads your PDF into the browser.

Tips & Warnings

  • When you move your ASP.NET pages into production, you will probably obtain the path name to a PDF in a variety of ways. For example, you might have the user enter a URL, such as "http://www.whitehouse.gov/taxes.pdf" in a text box. You could also display a file selection menu, offer a choice of PDFs from a drop-down menu or even load a PDF of your choice automatically. Regardless of how you obtain the path name to the PDF, pass that name to the "Response.Redirect" method as shown in these steps.

Related Searches:

References

Resources

Comments

Related Ads

Featured