Loading and saving documents

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.

Demo

Preview:

The DocumentViewer can be used to view documents in read-only mode. To edit documents cross-browser, cross-platform, the HTML5 Web editor can be used.

Sources

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tx_aspnet_samples.Basics
{
    public partial class loadsave : System.Web.UI.Page
    {
        private TXTextControl.DocumentServer.DocumentController documentController1;
        private TXTextControl.ServerTextControl serverTextControl1;
        private System.ComponentModel.IContainer components;

        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 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();
        }
    }
}

ServerTextControl Basics