The example adds a Power Point item to the list of available export formats in the Web Document Viewer and implements the custom ExportToPowerPoint method.
Note: The resulting document is exported by rasterizing each page to an image / slide (no native PowerPoint inner elements). This example uses the
Microsoft.Office.Interop.PowerPoint
library, which requires a local installation of PowerPoint. You can also implement a similar solution with any other library that supports PowerPoint document generation.
The CustomizeMenuActions
event is handled to add a custom menu item.
The application registers a CustomOperationLogger
class as the WebDocumentViewerOperationLogger service. The CustomOperationLogger
class overrides the ExportDocumentStarting method to call the ExportToPowerPoint
method when the user initializes export to Power Point.
You have to install the Office Developer Tools for Visual Studio to build the project, and have a local Microsoft Office (PowerPoint) installation to run the project.
Files to Review
- CustomOperationLogger.cs (CustomOperationLogger.vb)
- Presentation.cs (CustomOperationLogger.vb)
- Global.asax.cs (Global.asax.vb)
- Viewer.aspx (Viewer.aspx)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.XtraPrinting;
using DevExpress.XtraReports.Web.ClientControls;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
namespace PowerPointExport
{
public class CustomOperationLogger : WebDocumentViewerOperationLogger
{
public override ExportedDocument ExportDocumentStarting(string documentId, string asyncExportOperationId, string format, ExportOptions options, PrintingSystemBase printingSystem, Func<ExportedDocument> doExportSynchronously)
{
if (format == "powerPoint")
{
return new ExportedDocument(ExportToPowerPoint(printingSystem, documentId), @"application/vnd.ms-powerpoint", printingSystem.Document.Name + ".ppt");
}
return base.ExportDocumentStarting(documentId, asyncExportOperationId, format, options, printingSystem, doExportSynchronously);
}
byte[] ExportToPowerPoint(PrintingSystemBase printingSystem, string documentId)
{
// To the temp folder (for demo purposes)
var tempFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/PowerPoint/");
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
// image export options
ImageExportOptions exportOptions = new ImageExportOptions
{
ExportMode = ImageExportMode.SingleFilePageByPage,
Format = ImageFormat.Png,
PageBorderColor = Color.White,
Resolution = 600
};
// PowerPoint Presentation
Size size = CalculateSize(printingSystem);
Presentation p = new Presentation(size);
// go through each page
for (int i = 0; i < printingSystem.Pages.Count; i++)
{
// export image
var file = string.Format(@"{0}\{1}_{2}.png", tempFolder, documentId, i);
exportOptions.PageRange = (i + 1).ToString();
printingSystem.ExportToImage(file, exportOptions);
// add the image to the presentation
p.AddPage(file);
// clean up!
File.Delete(file);
}
// save presentation
string resultFile = string.Format(@"{0}\{1}.ppt", tempFolder, documentId);
p.SaveAs(resultFile);
byte[] document = File.ReadAllBytes(resultFile);
File.Delete(resultFile);
return document;
}
Size CalculateSize(PrintingSystemBase printingSystem)
{
SizeF size = printingSystem.Pages[0].PageSize.ToSize();
return GraphicsUnitConverter.Convert(size, GraphicsUnit.Document.ToDpi(), GraphicsUnit.Point.ToDpi()).ToSize();
}
}
}
Visual BasicImports System
Imports System.IO
Imports System.Drawing
Namespace PowerPointExport
Public Class Presentation
Private _powerPoint As Microsoft.Office.Interop.PowerPoint.Application
Private _presentation As Microsoft.Office.Interop.PowerPoint.Presentation
Protected Overrides Sub Finalize()
_powerPoint = Nothing
_presentation = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
Public Sub New(ByVal paperSize As Size)
_powerPoint = New Microsoft.Office.Interop.PowerPoint.Application()
_presentation = _powerPoint.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse)
_presentation.PageSetup.SlideHeight = paperSize.Height
_presentation.PageSetup.SlideWidth = paperSize.Width
End Sub
Private _slideNo As Integer = 0
Public Sub AddPage(ByVal fileName As String)
_slideNo += 1
Dim slide = _presentation.Slides.Add(_slideNo, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank)
slide.Shapes.AddPicture(fileName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, 0, 0)
End Sub
Public Sub SaveAs(ByVal filename As String)
If File.Exists(filename) Then File.Delete(filename)
_presentation.SaveAs(filename)
_presentation.Close()
_powerPoint.Quit()
End Sub
End Class
End Namespace
C#using System;
using System.IO;
using System.Web;
using DevExpress.XtraReports.Web;
using DevExpress.XtraReports.Web.WebDocumentViewer;
namespace PowerPointExport {
public class Global_asax : System.Web.HttpApplication {
void Application_Start(object sender, EventArgs e) {
System.Web.Routing.RouteTable.Routes.MapPageRoute("defaultRoute", "", "~/Default.aspx");
DevExpress.XtraReports.Web.WebDocumentViewer.Native.WebDocumentViewerBootstrapper.SessionState = System.Web.SessionState.SessionStateBehavior.Default;
DefaultWebDocumentViewerContainer.RegisterSingleton<WebDocumentViewerOperationLogger, CustomOperationLogger>();
System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
ASPxWebDocumentViewer.StaticInitialize();
DevExpress.XtraReports.Web.ClientControls.LoggerService.Initialize((ex, message) => System.Diagnostics.Debug.WriteLine("[{0}]: Exception occurred. Message: '{1}'. Exception Details:\r\n{2}", DateTime.Now, message, ex));
DevExpress.Web.ASPxWebControl.CallbackError += new EventHandler(Application_Error);
}
void Application_End(object sender, EventArgs e) {
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e) {
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e) {
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e) {
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
}
}
ASPx<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeBehind="Viewer.aspx.cs" Inherits="PowerPointExport.Viewer" %>
<%@ Register Assembly="DevExpress.XtraReports.v24.2.Web.WebForms, Version=24.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.XtraReports.Web" TagPrefix="dx" %>
<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function CustomizeMenuActions(s, e) {
const actionExportTo = e.GetById(DevExpress.Reporting.Viewer.ActionId.ExportTo);
const newFormat = { format: 'powerPoint', text: 'Power Point' };
if (actionExportTo) {
actionExportTo.events.on('propertyChanged', (args) => {
const formats = actionExportTo.items[0].items;
if (args.propertyName === 'items' && formats.indexOf(newFormat) === -1) {
formats.push(newFormat);
}
});
}
}
</script>
<dx:ASPxWebDocumentViewer ID="ASPxWebDocumentViewer1" runat="server">
<ClientSideEvents CustomizeMenuActions="CustomizeMenuActions" />
</dx:ASPxWebDocumentViewer>
</asp:Content>