ASP.NET PDF Web Viewer
How to open Microsoft Word (.docx) with outline content generated using C# in ASP.NET
Easy to upload, open, view, display Office Word document in EdgePDF, and dynamically generated PDF bookmarks based on Word document outline content in ASP.NET
About this tutorial
This tutorial page will setup an ASP.NET web site based on EdgePDF asp.net pdf viewer control,
which allows users to open, view Microsoft Word document with outline created in web browser.
How to open, view Office Word file with outline generated online using C#, asp.net
Using EdgePDF, you can allow your web user to
read, open, view, markup Microsoft Word documents online in web browser, and read Word outline content, convert to PDF bookmark data.
The following steps and code snippet shows you how to open, read, view Microsoft Word document with outline created in EdgePDF using C#, asp.net
- Setup EdgePDF demo project in IIS
- In demo project UserCommandProcessHandler.ashx, load Word document with outline in EdgePDF
The following source code shows how to read Word document with outline data in UserCommandProcessHandler.ashx
public byte[] ApplyAddonWordInOpenFunction(String srcFilePath)
{
// Load source .docx file.
DOCXDocument doc = new DOCXDocument(srcFilePath);
// Set strategy used to create outline entries for the document.
// Default: OutlineMode.ByOutlineLevel
doc.OutlineSetting.Mode = OutlineMode.ByHeading;
// Set the maximum level of entires in the outline.
// Valid range: 1 ~ 9; default value: 3
doc.OutlineSetting.MaxLevel = 2;
// 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 read, view Word document with outline in EdgePDF in the downloaded package
/DemoProjects/EdgePDF Demo Project/RasterEdge_Resource_Files/Tutorials/UserCommandProcessHandler-addon-word.ashx
To use the above tutorial, you need copy the UserCommandProcessHandler-addon-word.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.Word;
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(".docx"))
throw new Exception("Source file must be a .docx file.");
byte[] dataBytes = ApplyAddonWordInOpenFunction(@"C:\\temp\" + docid);
String fileName = docid.Replace(".docx", ".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
{
pdfDoc.Save(@"C:\temp\" + paraFilepathValue);
}
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[] ApplyAddonWordInOpenFunction(String srcFilePath)
{
// Load source .docx file.
DOCXDocument doc = new DOCXDocument(srcFilePath);
// Set strategy used to create outline entries for the document.
// Default: OutlineMode.ByOutlineLevel
doc.OutlineSetting.Mode = OutlineMode.ByHeading;
// Set the maximum level of entires in the outline.
// Valid range: 1 ~ 9; default value: 3
doc.OutlineSetting.MaxLevel = 2;
// 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();
}
}
}