OfficeViewer
Namespace: Wisej.Web.Ext.OfficeViewer
Assembly: Wisej.Web.Ext.OfficeViewer (4.1.0.0)
Represents a panel that displays Microsoft Office documents (Word, Excel, PowerPoint) using the Microsoft Office Online viewer. See view-office-documents-online.
- C#
- VB.NET
public class OfficeViewer : IFramePanel, IWisejHandler
Public Class OfficeViewer
Inherits IFramePanel
Implements IWisejHandler
The document is rendered inside an IFrame that loads https://view.officeapps.live.com/op/view.aspx. Microsoft's servers download the file from the URL passed to the viewer, therefore the file (or the Wisej application when using a relative FileSource or a FileStream) must be reachable from the public internet. Documents served from localhost or an intranet cannot be displayed.
Relative paths and streams are served by the control itself through its Wisej service URL; handle FileRequested to write the response yourself.
Example:
Displaying a public document:
var viewer = new OfficeViewer
{
Dock = DockStyle.Fill,
FileSource = "https://www.example.com/files/report.docx"
};
this.Controls.Add(viewer);
Constructors
OfficeViewer()
Initializes a new instance of OfficeViewer.
Properties
FileName
String: Returns or sets the file name with extension to return to the office viewer. (Default: "")
This property is required when using FileStream instead of FileSource, and must be set before assigning the stream. When FileSource is set, the file name is taken from FileSource and this property is ignored.
FileSource
String: Returns or sets the path of the Office file to view. It can be a relative path or an absolute URL. (Default: "")
An absolute http: or https: URL is passed directly to the Office Online viewer. Any other value is treated as a path relative to the application's root folder (resolved with MapPath) and the file is streamed to the viewer by this control.
Setting this property fires FileSourceChanged, clears FileStream and updates Url. Setting it to null is the same as setting it to an empty string.
Example:
Showing a file stored in the application's folder, or a file hosted elsewhere:
// relative to the application root, served by the Wisej application.
this.officeViewer1.FileSource = "Documents/Budget.xlsx";
// absolute URL, downloaded directly by the Office Online viewer.
this.officeViewer1.FileSource = "https://www.example.com/files/Presentation.pptx";
FileStream
Stream: Returns or sets the stream of the Office file to view.
FileName must be set, including the file extension, before assigning a stream; otherwise an InvalidOperationException is thrown. The extension is used by the Office Online viewer to detect the document type.
Assigning a stream clears FileSource. The stream must remain open and readable: it is rewound (when seekable) and copied to the response each time the viewer requests the file.
Throws:
- InvalidOperationException The value is not null and FileName is empty.
Example:
Displaying a document generated in memory:
var stream = new MemoryStream();
CreateInvoice(stream);
this.officeViewer1.FileName = "Invoice.docx";
this.officeViewer1.FileStream = stream;
Url
String: Returns or sets the source URL of the IFrame. (Default: "")
This property is managed by the control: it is overwritten with the Office Online viewer URL whenever FileSource or FileStream change. Use those properties instead.
Events
FileRequested
HandledEventHandler Fired when the file is requested.
The application can handle this event and override the default functionality by setting the Handled property to true.
The current response is available using Current.
FileSourceChanged
EventHandler Fired when the FileSource property is changed.