Non-Visual Document Automation

The ServerTextControl class implements a component that provides high-level text processing features for server-based applications. It is inherited from the System.ComponentModel.Component class. This class can be used to create, load, modify and save documents in various industry standard word processing formats.

Typical applications for ServerTextControl are document creation, conversion and automation processes. Whenever documents are created server-side or MS Office Automation should be replaced, the ServerTextControl component can be used.

Getting Started

This chapter shows how to create a small ASP.NET application from scratch with just a few lines of code. An instance of ServerTextControl is created, text is inserted, formatted and returned in HTML format.

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\ServerTextControl\
VB.NET %USERPROFILE%\Documents\TX Text Control 28.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Documentation Tutorials\ServerTextControl\

Used TX Text Control classes:

Creating the Application

1. Assuming that you have already run the TX Text Control .NET Server for ASP.NET installation program, start Visual Studio and create a new ASP.NET Web Application.

Image

2. Choose Add New Item... from the Project main menu. In the opened dialog box Add New Item, select Web Form and confirm with OK.

Image

In order to add non-visual components such as ServerTextControl to the Web form, the Component Designer surface of the ASPX page must be opened.

3. Select the newly created Web form in the Solution Explorer and choose Component Designer from the View main menu. Find the ServerTextControl in your Toolbox and drag and drop an instance onto the Component Designer view.

Image

4. Open the Designer of the Web form using the Designer menu item of the View main menu. Add a Button control from the Toolbox to the Web form and double-click it to open the code-behind event handler.

Insert the following code to the button click event:

using (serverTextControl1)
{
    serverTextControl1.Create();

    serverTextControl1.Text = "TX Text Control .NET Server for ASP.NET";

    TXTextControl.Selection selection = new TXTextControl.Selection(0, 15);
    selection.Bold = true;
    selection.FontSize = 300;

    serverTextControl1.Selection = selection;

    string data;
    serverTextControl1.Save(out data, TXTextControl.StringStreamType.HTMLFormat);
    Response.Write(data);
}
Using serverTextControl1
    serverTextControl1.Create()

    serverTextControl1.Text = "TX Text Control .NET Server for ASP.NET"

    Dim selection As New TXTextControl.Selection(0, 15)
    selection.Bold = True
    selection.FontSize = 300

    serverTextControl1.Selection = selection

    Dim data As String
    serverTextControl1.Save(data, TXTextControl.StringStreamType.HTMLFormat)
    Response.Write(data)
End Using

In the same Code view, call the InitializeComponent() in the Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
    InitializeComponent();
}
Protected Sub Page_Load(sender As Object, e As EventArgs)
    InitializeComponent()
End Sub

Press F5 to compile and start the application. After clicking the button, the TX Text Control .NET Server creates the document and returns HTML text.

ServerTextControl should be used in a Using statement as an explicit object disposing is required.

Example - Creating an Instance

ServerTextControl is a non-UI .NET component that can be initialized in all .NET based applications.

This example is part the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

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

You will find the source code and the ASPX page of this sample in the Solution structure tree under: Basics\create.aspx

Image

Example Description

1. Click the button "Create ServerTextControl" to create an instance of ServerTextControl.

Image

On the button click event, a new ServerTextControl instance is created. Formatted text is inserted using the Selection object and saved as HTML which is returned to a DIV element.

protected void Button1_Click(object sender, EventArgs e)
{
    // create a new instance of ServerTextControl
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        // ServerTextControl must be "created"
        tx.Create();
        tx.Text = "TX Text Control .NET Server for ASP.NET
This HTML text " +
            "has been created by ServerTextControl at " +
            DateTime.Now.ToString() + ".";

        // create a new selection and apply some formatting
        TXTextControl.Selection selection = new TXTextControl.Selection(0, 15);
        selection.Bold = true;
        selection.ForeColor = Color.Red;

        // apply the selection to the ServerTextControl instance
        tx.Selection = selection;

        // save the content as HTML
        string sHTML;
        tx.Save(out sHTML, TXTextControl.StringStreamType.HTMLFormat);

        htmldiv.InnerHtml = sHTML;
    }
}
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    ' create a new instance of ServerTextControl
    Using tx As New TXTextControl.ServerTextControl()
        ' ServerTextControl must be "created"
        tx.Create()
        tx.Text = "TX Text Control .NET Server for ASP.NET" & _
            vbCr & vbLf & "This HTML text " + _
            "has been created by ServerTextControl at " + _
            DateTime.Now.ToString() + "."

        ' create a new selection and apply some formatting
        Dim selection As New TXTextControl.Selection(0, 15)
        selection.Bold = True
        selection.ForeColor = Color.Red

        ' apply the selection to the ServerTextControl instance
        tx.Selection = selection

        ' save the content as HTML
        Dim sHTML As String
        tx.Save(sHTML, TXTextControl.StringStreamType.HTMLFormat)

        htmldiv.InnerHtml = sHTML
    End Using
End Sub

Example - Loading and Saving

TX Text Control supports various industry standard document formats such as DOC, DOCX, RTF, HTML, PDF and XML. All of these document formats can be created, converted or modified.

This sample shows how documents can be loaded and saved using ServerTextControl. When clicking on Load DOCX document, a DOCX file is loaded into an instance of ServerTextControl and saved into a byte array to be loaded into the DocumentViewer. The document is converted to PDF when the second button Download as PDF is clicked.

This example is part the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

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

You will find the source code and the ASPX page of this sample in the Solution structure tree under: Basics\loadsave.aspx

Image

Example Description

1. Click the button Load DOCX document in order to load a sample document into the ASP.NET DocumentViewer.

Image

The used DocumentViewer is part of TX Text Control .NET Server for ASP.NET and can be used to view documents in read-only mode. The DOCX document is loaded into an instance of ServerTextControl. To show the variety of saving options, this demo saves the document as a byte array into a memory variable which is then loaded into the DocumentViewer.

protected void Button1_Click(object sender, EventArgs e)
{
    // create a new instance of ServerTextControl
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        // load the document
        tx.Create();
        tx.Load(Server.MapPath("/documents/sample.docx"),
            TXTextControl.StreamType.WordprocessingML);

        // save the document in the internal format to a byte array
        byte[] data;
        tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        // load the byte array into the read-only DocumentViewer
        DocumentViewer1.LoadDocumentFromMemory(data,
            TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat);
    }

    btn_DownloadDocument.Visible = true;
}
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    ' create a new instance of ServerTextControl
    Using tx As New TXTextControl.ServerTextControl()
        ' load the document
        tx.Create()
        tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

        ' save the document in the internal format to a byte array
        Dim data As Byte()
        tx.Save(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)

        ' load the byte array into the read-only DocumentViewer
        DocumentViewer1.LoadDocumentFromMemory(data, TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat)
    End Using

    btn_DownloadDocument.Visible = True
End Sub

2. Click the button Download as PDF to download the loaded document as a PDF document.

The document is again loaded into an instance of ServerTextControl and then saved to memory into a byte array in the Adobe PDF format. This byte array is returned to the browser as a file download.

protected void Button2_Click(object sender, EventArgs e)
{
    byte[] data;

    // create a new instance of ServerTextControl and load the document
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(Server.MapPath("/documents/sample.docx"),
            TXTextControl.StreamType.WordprocessingML);

        // save the document as PDF into a byte array
        tx.Save(out data, TXTextControl.BinaryStreamType.AdobePDF);
    }

    // return the document to the browser for download
    Response.Clear();
    Response.AddHeader("content-disposition",
        String.Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"));
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(data);
    Response.End();
}
Protected Sub Button2_Click(sender As Object, e As EventArgs)
    Dim data As Byte()

    ' create a new instance of ServerTextControl and load the document
    Using tx As New TXTextControl.ServerTextControl()
        tx.Create()
        tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

        ' save the document as PDF into a byte array
        tx.Save(data, TXTextControl.BinaryStreamType.AdobePDF)
    End Using

    ' return the document to the browser for download
    Response.Clear()
    Response.AddHeader("content-disposition", [String].Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"))
    Response.ContentType = "application/pdf"
    Response.BinaryWrite(data)
    Response.[End]()
End Sub

Example - Creating PDF Document

TX Text Control .NET Server for ASP.NET includes a powerful PDF engine to create PDF documents in a very fast and reliable manner.

This demo shows how to convert a DOCX document to PDF and how to set various document access permissions using the SaveSettings object.

This example is part the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

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

You will find the source code and the ASPX page of this sample in the Solution structure tree under: Basics\pdf.aspx

Image

Example Description

1. Check some of the checkboxes and type in a security password for the created Adobe PDF document.

2. Click the button Create PDF.

Image

On clicking the button, a document is loaded into an instance of ServerTextControl. A new set of DocumentAccessPermissions is created that defines the access permissions of the PDF document. Based on the selected checkboxes, these settings are adjusted and passed to the Save method in the SaveSettings parameter.

Finally, the created PDF document is returned as a file download to the browser.

protected void btnCreate_Click(object sender, EventArgs e)
{
    // create a new instance of ServerTextControl
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();

        // load the sample document
        tx.Load(Server.MapPath("/documents/sample.docx"),
            TXTextControl.StreamType.WordprocessingML);

        TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings();

        // set the document access permissions based on the checkboxes
        TXTextControl.DocumentAccessPermissions permissions =
            new TXTextControl.DocumentAccessPermissions();


        if (cbAllowHighLevelPrinting.Checked == true)
            permissions =
                TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting;

        if (cbAllowDocumentAssembly.Checked == true)
            permissions = permissions |
                TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting;

        if (cbAllowExtractContents.Checked == true)
            permissions = permissions |
                TXTextControl.DocumentAccessPermissions.AllowExtractContents;

        if (cbAllowGeneralEditing.Checked == true)
            permissions = permissions |
                TXTextControl.DocumentAccessPermissions.AllowGeneralEditing;

        saveSettings.DocumentAccessPermissions = permissions;

        saveSettings.MasterPassword = tbPassword.Text;
        saveSettings.UserPassword = tbPassword.Text;

        // save the document as PDF in a byte array
        byte[] data;
        tx.Save(out data, TXTextControl.BinaryStreamType.AdobePDF, saveSettings);

        // return the document to the browser for download
        Response.Clear();
        Response.AddHeader("content-disposition",
            String.Format("attachment;filename={0}", "txtextcontrol.pdf"));
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(data);
        Response.End();
    }
}
Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
    ' create a new instance of ServerTextControl
    Using tx As New TXTextControl.ServerTextControl()
        tx.Create()

        ' load the sample document
        tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

        Dim saveSettings As New TXTextControl.SaveSettings()

        ' set the document access permissions based on the checkboxes
        Dim permissions As New TXTextControl.DocumentAccessPermissions()


        If cbAllowHighLevelPrinting.Checked = True Then
            permissions = TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting
        End If

        If cbAllowDocumentAssembly.Checked = True Then
            permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting
        End If

        If cbAllowExtractContents.Checked = True Then
            permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowExtractContents
        End If

        If cbAllowGeneralEditing.Checked = True Then
            permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowGeneralEditing
        End If

        saveSettings.DocumentAccessPermissions = permissions

        saveSettings.MasterPassword = tbPassword.Text
        saveSettings.UserPassword = tbPassword.Text

        ' save the document as PDF in a byte array
        Dim data As Byte()
        tx.Save(data, TXTextControl.BinaryStreamType.AdobePDF, saveSettings)

        ' return the document to the browser for download
        Response.Clear()
        Response.AddHeader("content-disposition", [String].Format("attachment;filename={0}", "txtextcontrol.pdf"))
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(data)
        Response.[End]()
    End Using
End Sub