Example E5101
Visible to All Users

PDF Document API for ASP.NET MVC - How to implement a PDF viewer

This example demonstrates how to use the Office File API and ASP.NET MVC Extensions to implement a custom PDF viewer. This PDF viewer displays a PDF document's content as images and allows users to navigate through document pages.

You need an active license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use the Office File API library in production code.

PDF viewer control

Overview

Use the following classes to implement the PDF viewer:

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

E5101/Controllers/PdfViewerController.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using DevExpress.Web.Mvc; using E5101.Models; using DevExpress.Pdf; using System.IO; namespace E5101.Controllers { public class PdfViewerController : Controller { public const string SESSION_KEY = "PdfFile"; public ActionResult PreviewByFileName(string pdfFileName) { Session[SESSION_KEY] = System.IO.File.ReadAllBytes(Server.MapPath(pdfFileName)); return PartialView("_PdfViewerPartial"); } public ActionResult Preview() { return PartialView("_PdfViewerPartial"); } public ActionResult DocumentViewPartial() { PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor(); MemoryStream stream = new MemoryStream((byte[])Session[SESSION_KEY]); documentProcessor.LoadDocument(stream); List<PdfPageModel> model = new List<PdfPageModel>(); for (int pageNumber = 1; pageNumber <= documentProcessor.Document.Pages.Count; pageNumber++) { model.Add(new PdfPageModel(documentProcessor) { PageNumber = pageNumber }); } return PartialView("_DocumentViewPartial", model); } } }
E5101/Models/PdfPageModel.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; using DevExpress.Pdf; using System.Drawing.Imaging; namespace E5101.Models { public class PdfPageModel { PdfDocumentProcessor _documentProcessor; public PdfPageModel(PdfDocumentProcessor documentProcessor) { this._documentProcessor = documentProcessor; } public PdfDocumentProcessor DocumentProcessor { get { return _documentProcessor; } } public int PageNumber { get; set; } public byte[] GetPageImageBytes() { using (Bitmap bitmap = DocumentProcessor.CreateBitmap(PageNumber, 900)) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); return stream.ToArray(); } } } } }
E5101/Views/PdfViewer/_DocumentViewPartial.cshtml
Razor
@using E5101.Models; @model List<PdfPageModel> @Html.DevExpress().DataView(settings => { settings.Name = "DocumentView"; settings.CallbackRouteValues = new { Controller = "PdfViewer", Action = "DocumentViewPartial" }; settings.SettingsTableLayout.RowsPerPage = 1; settings.SettingsTableLayout.ColumnCount = 1; settings.PagerSettings.AllButton.Visible = true; settings.SetItemTemplateContent(c => { PdfPageModel pageModel = c.DataItem as PdfPageModel; Html.DevExpress().BinaryImage(imageSettings => { imageSettings.Name = "bimPdfPage" + pageModel.PageNumber; }).Bind(pageModel.GetPageImageBytes()).Render(); }); }).Bind(Model).GetHtml()

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.