Getting Started with PDF Viewer Control for ASP.NET
|
|
Overview
|
|
The Winnovative PDF Viewer Control for ASP.NET can be used in a ASP.NET application
to display a PDF document inside a ASP.NET page and allows 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 Winnovative 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
|
|
First of all you have to add the control from PdfViewerAspNet.dll to the Visual
Studio toolbox. To do this you right click on the toolbox panel and select
'Choose Items'. You 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 you simply drag and drop the PdfViewer control from the
toolbox on the web page like you would do with any other server control. As 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>
|
|
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 produce by other tools like
our PDF converter library, as you can see in the sample ASP.NET application from
the downloaded 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 a sample C# 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:
1: protected void btnConvert_Click(object sender, EventArgs e)
2: {
3: string urlToConvert = textBoxURLToConvert.Text.Trim();
4: byte[] pdfBytes = new HtmlToPdfConverter().ConvertUrl(urlToConvert);
5: PdfViewer1.LicenseKey =
"jv5MIS071Ff6wtSNTyQtYqQIIFt/0AuR+QW8s/fFzuJzU4WI1daYrw6LjI1FtxTP";
6: PdfViewer1.PdfSourceBytes = pdfBytes;
7: }
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 C# code we are using in the demo ASP.NET application to display the
PDF document from a specified URL when a button is pressed:
1: protected void btnDisplayPDF_Click(object sender, EventArgs e)
2: {
3: PdfViewer1.LicenseKey =
"jv5MIS071Ff6wtSNTyQtYqQIIFt/0AuR+QW8s/fFzuJzU4WI1daYrw6LjI1FtxTP";
4: PdfViewer1.PdfSourceURL = textBoxURLToDisplay.Text.Trim();
5: }
|
|
Running the Samples
|
|
The samples are located in the Samples folder. To run the samples, open the projects
with the Microsoft Visual Studio and run them from there.
|
|
Licensing
|
|
The LicenseKey property of the PdfViewer class should
be set with the string you received after the purchase of the product.
|
|
Links
|
Winnovative PDF Viewer
for ASP.NET Home Page
|