Click or drag to resize
Getting Started with ExpertPdf PDF Viewer Control for ASP.NET

The ExpertPdf PDF Viewer Control for ASP.NET can be used in a ASP.NET 2.0 or ASP.NET 4.0 application to display a PDF document inside an ASP.NET page and allow the application users to browse and print the PDF document. The source of the PDF document can be a URL or a stream of bytes represented as a byte[] object. The viewer requires Adobe Reader to be installed on the client computer in order to work properly.

The PDF Viewer Control for ASP.NET is delivered as a single assembly PdfViewerAspNet.dll. The main class defined by this assembly is the PdfViewer class which represents the ASP.NET control.

Adding the PDF Viewer Control to the Microsoft Visual Studio Toolbox

To add the control from PdfViewerAspNet.dll to the Visual Studio Toolbox, right click on the Toolbox panel while you are in the design mode of a Web Form and select 'Choose Items'. Browse to the PdfViewerAspNet.dll and the PdfViewer control should have been added in the Toolbox panel. To use the control in a web page of your ASP.NET application, simply drag and drop the PdfViewer control from the toolbox on the web page. As a result of this, a reference to the PdfViewerAspNet.dll is added to your application and the following line is added to your web page:

<%@ Register Assembly="PdfViewerAspNet" Namespace="PdfViewer4AspNet" TagPrefix="cc1" %>

A reference to the PdfViewer4AspNet namespace is also added. At this moment you are ready to use the control and display PDF documents inside your ASP.NET page.

Displaying PDF Documents from Streams

In order to display a PDF document represented as a stream of bytes inside your ASP.NET page, you have to register a HTTP handler defined by the PDF Viewer control in the Web.config file of your application. This can be done by adding the handler definition in the <system.web> section of the Web.config configuration file as in the example below:

<system.web>
      <httpHandlers>
        <add verb="*" path="PdfBytesHandler.axd" type="PdfViewer4AspNet.PdfSourceBytesHandler,PdfViewerAspNet"></add>
      </httpHandlers>
</system.web>

In IIS 7+ Integrated Pipeline Mode, the syntax and location in Web.config for declaring a HTTP handler was changed like below:

<!-- IIS 7+ Integrated Pipeline Mode -->
<system.webServer>
    <handlers>
      <add name="PdfBytesHandler" verb="*" path="PdfBytesHandler.axd" type="PdfViewer4AspNet.PdfSourceBytesHandler,PdfViewerAspNet" resourceType="Unspecified"/>
    </handlers>
</system.webServer>

We provide the Web.IIS7.config file in the demo application folder that can be renamed to Web.config in order to run the demo application in IIS7+ Integrated Pipeline Mode.

The next step is to set the PdfSourceBytes property of the control with a byte[] object representing the binary image of the PDF document. The byte[] stream can be taken from PDF file or can be produced by other tools like our HTML to PDF converter library, as you can see in the sample ASP.NET application from the product archive. The byte[] object is stored in the session of the application, so there is no need to set the PdfSourceBytes property on each postback.

Here is some sample code, taken from our sample application, to convert the HTML code from a specified URL to PDF and load the stream into the PDF Viewer control when a button is pressed:

protected void btnConvert_Click(object sender, EventArgs e)
{
    //get the PDF stream
    string urlToConvert = textBoxURLToConvert.Text.Trim();
    byte[] pdfBytes = new PdfConverter().GetPdfFromUrlBytes(urlToConvert);

    // set the PDF bytes to be loaded in viewer
    // pdfViewerControl.LicenseKey = "your license key here";
    pdfViewerControl.PdfSourceBytes = pdfBytes;
}

There is no temporary file in the sample above and this can greatly simplify the deployment of your web applications on various hardware and software configurations.

Displaying PDF Documents from URLs

Displaying a PDF document from a specified URL is as simple as setting the PdfSourceURL property of the PdfViewer control with the URL of the PDF document to be displayed.

Here is the sample code we are using in the demo ASP.NET application to display the PDF document from a specified URL when a button is pressed:

protected void btnConvert_Click(object sender, EventArgs e)
{
    // pdfViewerControl.LicenseKey = "your license key here";
    pdfViewerControl.PdfSourceURL = url_to_pdf;
}
Licensing

The LicenseKey property of the PdfViewer class should be set with the license key string you have received after the product purchase.