Displaying a PDF in an ASP.NET MVC Web application |
Top Previous Next |
You are here: Gnostice PDFOne .NET Developer Guide > Getting Started > Displaying a PDF in an ASP.NET MVC Web application
Displaying a PDF in an ASP.NET MVC Web application In this topic, we will see how to display a PDF using the PDF viewer MVC extension with the Razor engine.
Here are the steps to follow: 1. Open Visual Studio and create a new "empty" MVC project.
2. Add references to the following DLLs: Gnostice.PDFOne.dll, Gnostice.PDFOne.Extensions.MVC.dll, Gnostice.PDFOne.dll Gnostice.XtremeFontEngine.dll, Gnostice.XtremeImageEngine.dll, and BouncyCastle.Crypto.dll. Select these DLLs and set their "Copy Local" properties to true.
4. Select the project and add a Global.asax ("Global Application Class") file as "a new item".
5. In the Application_Start() method, add the following line to activate the product license. Please use trial key if you are using the trial version. If you are using the registered version, use the key that was sent to you by e-mail when made the purchase order. Gnostice.PDFOne.Extensions.MVC.PDFViewer.ActivateLicense("your-license-key"); 6. In Solution Explorer, select "Controllers" and add a new empty "Home" controller.
7. Add a new default view for the Index() method of the controller (without any layout, model, or strongly typed view) and give it the name "Index".
8 Select "Scripts" and add jQuery to the project. 1.Drag and drop jQuery to the "head" section of the Index view (Index.cshtml). 2.Enter the following directives below the jQuery script tag so that the razor engine will load the JavaScript files of the PDFOne MVC PDF viewer control when the page is displayed in a browser. 3.In the DIV tag, enter these lines to render the PDF viewer control.
@{ PDFViewerSettings PDFViewerSettings1 = new PDFViewerSettings(); PDFViewerSettings1 = new PDFViewerSettings(); PDFViewerSettings1.Width = System.Web.UI.WebControls.Unit.Pixel(700); PDFViewerSettings1.Height = System.Web.UI.WebControls.Unit.Pixel(700); PDFViewerSettings1.ClientObjectName = "pdfviewer1"; Html.Gnostice().PDFViewer(PDFViewerSettings1).LoadDocument(Server.MapPath("~/App_Data/") + "sample_doc.pdf").Render(); }
9. Add a new "App_Data" folder to the project. 10. Copy a PDF file named "sample_doc.pdf" into the folder.
11. Open the Web.config file and add the following line above the closing "system.web" tag.
<httpModules> <add name="Gnostice.PDFOne.Extensions.MVC.PDFViewerHttpHandlerModule" type="PDFViewerHttpHandlerModule"/> </httpModules> 12. Above the closing tag of "system.webServer" tag, add the following lines.
<modules runAllManagedModulesForAllRequests="true"> <add type="Gnostice.PDFOne.Extensions.MVC.PDFViewerHttpHandlerModule" name="PDFViewerHttpHandlerModule" /> </modules> If there is no system.webServer tag, then add the following lines above the "runtime" tag.
<system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true"> <add type="Gnostice.PDFOne.Extensions.MVC.PDFViewerHttpHandlerModule" name="PDFViewerHttpHandlerModule" /> </modules> </system.webServer>
13. Build the project and then run it.
|