This example demonstrates how to use the Office File API and ASP.NET Web Forms Controls 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.
Overview
Use the following classes to implement the PDF viewer:
- PdfDocumentProcessor
Allows you to manage PDF files. The PdfDocumentProcessor's LoadDocument method opens a PDF document and the CreateBitmap method converts the document's pages to images. - ASPxBinaryImage
Displays a PDF document's pages as images. - ASPxDataView
Allows users to navigate through pages. - ASPxUploadControl
Allows users to upload a PDF document to the server. - ASPxCallbackPanel
Opens a document in the PDF viewer after a user uploads the PDF document to the server.
Files to Look At
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
- PdfViewer.ascx (VB: PdfViewer.ascx)
- PdfViewer.ascx.cs (VB: PdfViewer.ascx.vb)
Documentation
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
ASPx<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="E5095.Default" %>
<%@ Register TagPrefix="uc" TagName="PdfViewer" Src="~/PdfViewer.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Upload your PDF file (Max file size: 4Mb):
<dx:ASPxUploadControl ID="ucUploadPdf" runat="server" UploadMode="Auto" Width="280px"
ShowUploadButton="true" OnFileUploadComplete="ucUploadPdf_FileUploadComplete">
<ValidationSettings AllowedFileExtensions=".pdf" MaxFileSize="4194304">
</ValidationSettings>
<ClientSideEvents FileUploadComplete="function(s, e) { if (e.isValid) { callbackPanel.PerformCallback(); } }" />
</dx:ASPxUploadControl>
<dx:ASPxCallbackPanel ID="cbViewer" runat="server" ClientInstanceName="callbackPanel">
<PanelCollection>
<dx:PanelContent runat="server" SupportsDisabledAttribute="True">
<uc:PdfViewer ID="viewer" runat="server" PdfFilePath="FallCatalog.pdf" />
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
</div>
</form>
</body>
</html>
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace E5095 {
public partial class Default : System.Web.UI.Page {
protected void Page_Init(object sender, EventArgs e) {
if (Session["PdfFile"] != null) {
viewer.PdfData = (byte[]) Session["PdfFile"];
}
}
protected void ucUploadPdf_FileUploadComplete(object sender, DevExpress.Web.FileUploadCompleteEventArgs e) {
if (e.IsValid) {
Session["PdfFile"] = e.UploadedFile.FileBytes;
}
}
}
}
Code<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PdfViewer.ascx.cs" Inherits="E5095.PdfViewer" %>
<dx:ASPxLabel ID="lbErrorMessage" runat="server" ForeColor="Red">
</dx:ASPxLabel>
<dx:ASPxDataView ID="dvDocument" runat="server">
<SettingsTableLayout ColumnCount="1" RowsPerPage="1" />
<PagerSettings ShowNumericButtons="True">
<AllButton Visible="True">
</AllButton>
</PagerSettings>
<ItemTemplate>
<dx:ASPxBinaryImage ID="bimPdfPage" runat="server" OnDataBinding="bimPdfPage_DataBinding">
</dx:ASPxBinaryImage>
</ItemTemplate>
<ItemStyle>
<Paddings Padding="0px" />
</ItemStyle>
</dx:ASPxDataView>
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Pdf;
using System.IO;
using DevExpress.Web;
using System.Drawing;
using System.Drawing.Imaging;
namespace E5095 {
public partial class PdfViewer : System.Web.UI.UserControl {
byte[] _pdfData;
string _pdfFilePath;
PdfDocumentProcessor _documentProcessor;
public PdfViewer() {
_pdfData = null;
_pdfFilePath = "";
_documentProcessor = new PdfDocumentProcessor();
}
protected PdfDocumentProcessor DocumentProcessor {
get {
return _documentProcessor;
}
}
public Unit Width {
get {
return dvDocument.Width;
}
set {
dvDocument.Width = value;
}
}
public Unit Height {
get {
return dvDocument.Height;
}
set {
dvDocument.Height = value;
}
}
public string PdfFilePath {
get {
return _pdfFilePath;
}
set {
try {
_pdfFilePath = value;
if (!String.IsNullOrEmpty(value)) {
DocumentProcessor.LoadDocument(Server.MapPath(value), true);
BindDataView();
}
}
catch (Exception ex) {
ShowError(String.Format("File Loading Failed: {0}", ex.Message));
}
}
}
public byte[] PdfData {
get {
return _pdfData;
}
set {
try {
_pdfData = value;
if (value != null) {
using (MemoryStream stream = new MemoryStream(value)) {
DocumentProcessor.LoadDocument(stream, true);
BindDataView();
}
}
}
catch (Exception ex) {
ShowError(String.Format("File Loading Failed: {0}", ex.Message));
}
}
}
protected void BindDataView() {
if (DocumentProcessor.Document != null) {
List<PdfPageItem> data = new List<PdfPageItem>();
for (int pageNumber = 1; pageNumber <= DocumentProcessor.Document.Pages.Count; pageNumber++) {
data.Add(new PdfPageItem() {
PageNumber = pageNumber
});
}
dvDocument.DataSource = data;
dvDocument.DataBind();
}
lbErrorMessage.Text = String.Empty;
}
protected void bimPdfPage_DataBinding(object sender, EventArgs e) {
ASPxBinaryImage image = sender as ASPxBinaryImage;
DataViewItemTemplateContainer container = image.NamingContainer as DataViewItemTemplateContainer;
int pageNumber = (int)container.EvalDataItem("PageNumber");
using (Bitmap bitmap = DocumentProcessor.CreateBitmap(pageNumber, 900)) {
using (MemoryStream stream = new MemoryStream()) {
bitmap.Save(stream, ImageFormat.Png);
image.ContentBytes = stream.ToArray();
}
}
}
protected class PdfPageItem {
public int PageNumber {
get;
set;
}
}
protected void ShowError(string message) {
dvDocument.DataSource = null;
dvDocument.DataBind();
lbErrorMessage.Text = message;
}
}
}