ASP.NET Web Forms Document Viewer

Introduction

This chapter shows how to use the DocumentServer.Web.DocumentViewer control in ASP.NET projects to display documents of the supported types in Web browsers. When viewing documents in Web applications is the only requirement, the DocumentServer.Web.DocumentViewer comes as a simple alternative to the Web.TextControl. The DocumentServer.Web.DocumentViewer is part of the TXTextControl.DocumentServer.Web namespace.

The DocumentServer.Web.DocumentViewer is a ASP.NET server control that can be used to display documents in the Web browser in ASP.NET projects. The document handling is done by an instance of the DocumentServer.DocumentController component that must be bound to the control.

The cooperation of the DocumentServer.Web.DocumentViewer and the DocumentServer.DocumentController resemble that of the view and the controller parts of the well-known Model-view-controller (MVC) pattern, while the document plays the part of the model. This means the user never modifies the document directly. The controller acts as mediator between the view and the model. Thus, the user only sees what the controller routes from the document to the viewer, and only the actions that the controller routes back from the viewer are applied to the document. Thanks to this setup, it is possible to control which parts of the document the user sees and which actions the user is allowed to take up to a great deal of detail.

The DocumentServer.DocumentController and thus the DocumentServer.Web.DocumentViewer can handle all the document formats that can contain application fields. These formats are summarized in the DocumentServer.FileFormat enumeration.

Getting Started

This tutorial shows how to create a simple ASP.NET Web application project using the DocumentViewer component.

The source code for this simple example is located in the following directories:

Language Project Location
C# %USERPROFILE%\Documents\TX Text Control 28.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Documentation Tutorials\DocumentViewer\
VB.NET %USERPROFILE%\Documents\TX Text Control 28.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Documentation Tutorials\DocumentViewer\

Used TX Text Control classes:

Create Your Application

1. Open Visual Studio and create a new ASP.NET Empty Web Application.

Image

In the opened dialog Add New Item, select Web Form and confirm with Add.

Image

2. In the Solution Explorer, select the newly created Web Form and choose Designer from the View main menu.

Find the DocumentController component in the Toolbox and drag and drop an instance onto the Designer form.

Repeat the same with an instance of the ServerTextControl component.

Image

3. Select the DocumentController and find the TextComponent property in the Properties Window. Set the property value to serverTextControl1 - the name of the created ServerTextControl instance.

Image

4. In the Solution Explorer, select the newly created Web Form and choose Designer from the View main menu.

Find the DocumentViewer component in the Toolbox and drag and drop an instance onto the form.

Image

5. While the project is selected in the Solution Explorer, choose Add Existing Item... from the Project main menu.

In the opened dialog Add Existing Item, browse to the following sample folder:

%USERPROFILE%\Documents\TX Text Control 28.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Documentation Tutorials\DocumentViewer\

or

%USERPROFILE%\Documents\TX Text Control 28.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Documentation Tutorials\DocumentViewer\

Select the following file: demo.rtf and click the Add button.

Image

6. Switch to Code view using the Code menu item of the View main menu.

Add the following code to the Form_Load event:

protected void Page_Load(object sender, EventArgs e)
{
    InitializeComponent();
    DocumentViewer1.DocumentController = documentController1;
    DocumentViewer1.LoadDocument(Server.MapPath("Demo.rtf"));
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    InitializeComponent()
    DocumentViewer1.DocumentController = documentController1
    DocumentViewer1.LoadDocument(Server.MapPath("Demo.rtf"))
End Sub

Compile and start the application.