ASP.NET PDF Viewer Control
How to open, view Microsoft PowerPoint with bookmark online using C# in web browser
Easy to upload, open, view, display, markup Microsoft PowerPoint document online, and create PDF bookmark online
About this tutorial
This tutorial page will setup an ASP.NET web site which allows users to open, view, markup, redate Microsoft Office PowerPoint document, and create PDF bookmarks based PowerPoint content.
How to view, modify Microsoft PowerPoint document online with bookmark created using C#, asp.net
Using EdgePDF, you can allow your web user to
open, view, markup Office PowerPoint documents online in web browser, and create PDF bookmark based PowerPoint document content.
The following steps and code snippet shows you how to read, view PowerPoint with bookmark using C#, asp.net
- Setup EdgePDF demo project in IIS
- In demo project UserCommandProcessHandler.ashx, read PowerPoint with bookmark in EdgePDF
- Save new/updated PDF file on the web server
The following source code shows how to read, view PowerPoint with bookmark in UserCommandProcessHandler.ashx
public byte[] ApplyAddonPowerPointInOpenFunction(String srcFilePath)
{
// Load source .pptx file.
PPTXDocument doc = new PPTXDocument(srcFilePath);
// Enable bookmark flag to show bookmarks. Default: false.
doc.EnableBookmark = true;
// Convert to PDF file and return the data bytes of the result file.
using (MemoryStream ms = new MemoryStream())
{
doc.ConvertToDocument(DocumentType.PDF, ms);
return ms.ToArray();
}
}
Complete source code of UserCommandProcessHandler.ashx
You can find the complete source code for reading, viewing Office PowerPoint document with bookmark created in the downloaded package
/DemoProjects/EdgePDF Demo Project/RasterEdge_Resource_Files/Tutorials/UserCommandProcessHandler-addon-powerpoint.ashx
To use the above tutorial, you need copy the UserCommandProcessHandler-addon-powerpoint.ashx to the EdgePDF project location /RasterEdge_Resource_Files/, and
change file name to UserCommandProcessHandler.ashx
<%@ WebHandler Language="C#" Class="UserCommandProcessHandler" %>
using System;
using System.IO;
using System.Web;
using RasterEdge.Imaging.Basic;
using RasterEdge.XDoc.PowerPoint;
using RasterEdge.WDP;
using RasterEdge.XDoc.PDF.HTML5Editor;
public class UserCommandProcessHandler : ProcessHandler
{
/*
The SDK will use the following url parameter names, please do not use them
RECommand, src, filepath, restful
*/
public override PDFWebDocument FileProcess()
{
HttpRequest request = this.Context.Request;
if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
{
// load file
String docid = request.QueryString["yourtarget"];
try
{
if (String.IsNullOrEmpty(docid) || !docid.EndsWith(".pptx"))
throw new Exception("Source file must be a .pptx file.");
byte[] dataBytes = ApplyAddonPowerPointInOpenFunction(@"C:\\temp\" + docid);
String fileName = docid.Replace(".pptx", ".pdf");
return REProcessControl.PageLoadFile(request, this.Manager, dataBytes, fileName);
}
catch (Exception ex)
{
Logger.Log("FileProcess: fail to open file " + docid + ". " + ex.Message, LogType.ERROR);
return null;
}
}
else
{
Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);
// load default file. defined in Web.config
return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
}
}
// fid: Task ID.
// savePath: The abosolute path of the saved PDF file in the task's output folder.
// responseMsg: The response message for saving the current document to the task's output folder.
// The string value is the relative path of the output file. "Error" means that the saving process is failed.
public override void SaveFile_OnServer(String fid, String savePath, String responseMsg)
{
// You can process saved pdf document here, like add watermark, ...
var documentPath = savePath;
if (!String.IsNullOrEmpty(documentPath))
{
Logger.LogFile(fid, "SaveFileOnServer: output file path " + documentPath, LogType.DEBUG);
// To verify the output file.
RasterEdge.Imaging.Basic.BaseDocument document = null;
if (documentPath.EndsWith(".pdf"))
{
// document object includes user modified content
document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);
}
if (document != null)
{
RasterEdge.XDoc.PDF.PDFDocument pdfDoc = document as RasterEdge.XDoc.PDF.PDFDocument;
try
{
// Post-process
RasterEdge.XDoc.PDF.PDFMetadata metadata = pdfDoc.GetDescription();
metadata.Producer = "RasterEdge EdgePDF";
pdfDoc.SetDescription(metadata);
}
catch (Exception ex)
{
// Process error code, and return error information here
Logger.LogFile(fid, ex.Message + ". fail to do post-process.", LogType.ERROR);
}
// Get the upload information of the file
RasterEdge.WDP.DocUploadInfo docinfo = RasterEdge.WDP.Manager.FileManager.getUploadinfoByFid(fid);
// Get your file open url parameters value, if needed.
String paraFilepathValue = docinfo.GetRequestParameters("yourtarget");
if (!String.IsNullOrEmpty(paraFilepathValue))
{
try
{
// Replace file extension (".pptx") to ".pdf".
int index = paraFilepathValue.LastIndexOf('.');
if (index > 0)
paraFilepathValue = paraFilepathValue.Substring(0, index);
pdfDoc.Save(@"C:\temp\" + paraFilepathValue + ".pdf");
}
catch (Exception ex)
{
// Process error code, and return error information here
Logger.LogFile(fid, ex.Message + ". fail to save file to server.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "no 'yourtarget' in the HTTPRequest.", LogType.INFO);
}
}
else
{
Logger.LogFile(fid, "output PDF file is invalid.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "fail to save file to server. output file path is null or empty.", LogType.ERROR);
}
}
public byte[] ApplyAddonPowerPointInOpenFunction(String srcFilePath)
{
// Load source .pptx file.
PPTXDocument doc = new PPTXDocument(srcFilePath);
// Enable bookmark flag to show bookmarks. Default: false.
doc.EnableBookmark = true;
// Convert to PDF file and return the data bytes of the result file.
using (MemoryStream ms = new MemoryStream())
{
doc.ConvertToDocument(DocumentType.PDF, ms);
return ms.ToArray();
}
}
}