Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

ASP.NET MVC: Adding Protected Sections to Documents

A SubTextPart object represents a user-defined range of text in a TX Text Control document. A SubTextPart is basically a range of text with a Name and an ID property to store additional information. In the implementation of the MailMerge class, a SubTextPart is used for repeating blocks in a reporting template. But the SubTextParts can be used for many different applications such as adding comments or to store additional hidden information to text ranges. This sample shows how to insert a…

ASP.NET MVC: Adding Protected Sections to Documents

A SubTextPart object represents a user-defined range of text in a TX Text Control document. A SubTextPart is basically a range of text with a Name and an ID property to store additional information.

In the implementation of the MailMerge class, a SubTextPart is used for repeating blocks in a reporting template. But the SubTextParts can be used for many different applications such as adding comments or to store additional hidden information to text ranges.

This sample shows how to insert a SubTextPart to protect a section from being edited by the user. The ID property is used to mark the SubTextPart as editable (0) or non-editable (1). In order to insert this special SubTextPart, an HTTP Post method is implemented in the MVC controller:

[HttpPost]
public string InsertSubTextPart(string Name, string BinaryDocument, bool Protected)
{
    byte[] data = null;

    // create a temporary ServerTextControl to create the
    // SubTextPart
    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        // load the Selection from the Web.TextControl
        tx.Load(Convert.FromBase64String(BinaryDocument),
            BinaryStreamType.InternalUnicodeFormat);

        tx.SelectAll();
        int iTextLength = tx.Selection.Length;

        // create a new SubTextPart over the complete text
        SubTextPart part = new SubTextPart("txmb_" + Name, 
            Convert.ToInt32(Protected), 1, iTextLength);
        tx.SubTextParts.Add(part);

        // save the complete document
        tx.SelectAll();
        tx.Selection.Save(out data, BinaryStreamType.InternalUnicodeFormat);
    }

    // return the Selection as a Base64 encoded string
    return Convert.ToBase64String(data);
}

This method creates a ServerTextControl that is used to load the selected text of the Web.TextControl. This text is converted into a SubTextPart and saved in the internal Unicode format to be returned to the client.

On client side, using JavaScript, the above HTTP Post method InsertSubTextPart is called using a jQuery AJAX call. If the selection length is larger than 0, the selection is being saved and sent to the controller method. The returned Base64 encoded binary internal Unicode format is loaded back into the selection:

function InsertSubTextPart(name, protect) {
        // retrieve the JS Selection object
        var sel = TXTextControl.selection

        // check, if something has been selected
        sel.getBounds(function (e) {
                if (e.length == 0) {
                        alert('SubTextPart cannot be added - please select text.');
                        return;
                }
                else {
                        // save the current selection
                        TXTextControl.saveSelection(
                                TXTextControl.streamType.InternalUnicodeFormat,
                                function (e) {

                                        var serviceURL = "/Home/InsertSubTextPart";

                                        $.ajax({
                                                type: "POST",
                                                url: serviceURL,
                                                contentType: 'application/json',
                                                data: JSON.stringify({
                                                        Name: name,
                                                        BinaryDocument: e.data,
                                                        Protected: protect
                                                }),
                                                success: successFunc,
                                                error: errorFunc
                                        });

                                        function successFunc(data, status) {
                                                // load the created SubTextPart into the current Selection
                                                TXTextControl.loadSelection(
                                                        TXTextControl.StreamType.InternalUnicodeFormat, data);
                                        }

                                        function errorFunc() {
                                                alert('Error');
                                        }
                                });
                }
        })
}

If the caret is entering and leaving these SubTextParts, events are triggered that are used to set the editMode property of Web.TextControl either to ReadAndSelect or Edit mode.

// when entering the SubTextPart, check for the ID
// if 1 then set read and select
TXTextControl.addEventListener("subTextPartEntered", function (e) {
    if (e.id == '1') {
        TXTextControl.editMode = TXTextControl.EditMode.ReadAndSelect;
        $('#tbSubTextPartName').val(e.name);
    }
});

// set the edit mode to edit mode again
TXTextControl.addEventListener("subTextPartLeft", function (e) {
    TXTextControl.editMode = TXTextControl.EditMode.Edit;
    $('#tbSubTextPartName').val('');
});

In the following screenshot, you can see an activated SubTextPart which is protected. The ribbon bar is disabled as the content is not editable inside this protected section.

ASP.NET MVC: Adding protected sections to documents

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

TX Text Control .NET Server for ASP.NET

  • TXTextControl.Web.TextControl Class

TX Text Control .NET for Windows Forms

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.ServerTextControl Class
  • TXTextControl.SubTextPart Class
  • TXTextControl.SubTextPart.ID Property
  • TXTextControl.SubTextPart.Name Property
  • TXTextControl.TextControl.EditMode Property

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2015 or better
  • TX Text Control .NET Server (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETReportingGitHub

ASP.NET MVC: Implementing a Simplistic, Custom Button Bar

For some applications, the fully-featured ribbon bar might be too overloaded with features or the ribbon concept is not required in a project. Programmatically, all ribbon tabs, groups and buttons…


ASP.NETReportingElectronic Signature

ASP.NET: Adding Electronic Signatures to Documents

An electronic signature is in many processes legally sufficient to prove an identity. According to the U.S. Federal ESIGN Act passed in 2000, an electronic signature is an: Electronic sound,…


ASP.NETReportingGitHub

HTML5: Display and Handle FormCheckBox Fields

The Text Control Reporting engine MailMerge can populate fields automatically during the merge process. These fields can be also combined with MS Word compatible form fields such as checkboxes.…


ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.


ASP.NETReportingAuto Save

Automatically Reconnect to the Server and Recover the Document

We just published a sample project that shows how to reconnect to the server and how to recover the current document.