Products Technologies Demo Docs Blog Support Company

Windows Forms: Loading and Saving Documents from a Server

As part of our Flow Type Layout Reporting Consulting service, we received an interesting request. With Microsoft SharePoint in combination with Microsoft Word, you can open documents directly from your SharePoint website and load it into the local MS Word installation. After editing, the document can be saved back to the repository. In order to maintain templates for reporting applications, this approach is perfect in order to give users the full power of thick-client applications in…

Windows Forms: Loading and Saving Documents from a Server

As part of our Flow Type Layout Reporting Consulting service, we received an interesting request. With Microsoft SharePoint in combination with Microsoft Word, you can open documents directly from your SharePoint website and load it into the local MS Word installation. After editing, the document can be saved back to the repository.

In order to maintain templates for reporting applications, this approach is perfect in order to give users the full power of thick-client applications in combination with centralized documents and templates.

We have created a very basic demo project that shows this procedure. The solution consists of two parts the Web application and a client application to edit the document. The client application will be registered with a specific protocol. The browser is looking for registered protocols when the user is clicking a link. The protocol is connected to an application through the registry database.

You might have already seen some hyperlink protocols such as mailto:, callto: or skype:. For this purpose, we are creating our own protocol called ClientApplication. The following reg file must be created and double-clicked to add the values to the database.

REGEDIT4

[HKEY_CLASSES_ROOT\ClientApplication]
@="URL:ClientApplication Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\ClientApplication\shell]

[HKEY_CLASSES_ROOT\ClientApplication\shell\open]

[HKEY_CLASSES_ROOT\ClientApplication\shell\open\command]
@="\"C:\ClientApplication.exe\" \"%1\""

You can download the registry file here: ClientApplication.reg

Make sure that the path C:\\ClientApplication.exe\ matches the path on your machine for the primary output of the ClientApplication.

More information about these protocols can be found in this MSDN article:

Registering an Application to a URI Scheme

  1. After loading the Visual Studio 2012 solution, compile and start the Web application. The website is very simple and basically shows only 3 links to documents that are stored on the server. Pay attention to the status bar when hovering over a link. Our custom protocol ClientApplication: is shown.

    Load and save documents from a server
  2. Click on any document. All browsers will show a similar message box asking to confirm that an application on your computer is allowed to open this file type.

    Load and save documents from a server

    Confirm with Allow in order to load the document.

  3. Thanks to the protocol, the browser starts our associated ClientApplication which loads the document with a simple HttpWebRequest.

    Load and save documents from a server
  4. Make some changes to the document and click the green Save back button.

    Load and save documents from a server

When you repeat the steps and load the same document again, you will notice that the changes have been saved properly.

The ClientApplication receives command line arguments which is basically the name of the document to load. Simple HttpWebRequests are used to get the stream which is converted into a byte array and then loaded into TX Text Control.

static void Main(string[] args)
{
    string sFileLocation = args[0].Substring("ClientApplication:".Length);

    // create a new httpp request and load the document into a byte array
    HttpWebRequest myHttpWebRequest =
        (HttpWebRequest)HttpWebRequest.Create("http://" + sFileLocation);
    HttpWebResponse myHttpWebResponse =
        (HttpWebResponse)myHttpWebRequest.GetResponse();

    System.IO.Stream stream = myHttpWebResponse.GetResponseStream();

    byte[] data = null;

    // convert the stream to an array
    var memoryStream = new MemoryStream();
    stream.CopyTo(memoryStream);
    data = memoryStream.ToArray();

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // open the form and pass byte array and file location
    Application.Run(new Form1(data, sFileLocation));
}
private byte[] bDocument;
private string sFileLocation;

public Form1(byte[] data, string location)
{
    bDocument = data;
    sFileLocation = location;

    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    // load the document from the byte array
    textControl1.Load(bDocument,
        TXTextControl.BinaryStreamType.WordprocessingML);
}

Conclusion: Creating your own protocol is a very efficient way to load, modify and save documents directly from and to a Website using a thick-client Windows application. TX Text Control provides the required components for client-side and server-side processing.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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 2017 or better
  • TX Text Control .NET Server and TX Text Control .NET for Windows Forms

Related Posts

Windows FormsGetting StartedTutorial

Windows Forms Tutorial: Create Your First Windows Forms C# Application

This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.


ASP.NETASP.NET CoreMailMerge

How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…


AngularDocument EditorNode.js

Creating an Angular Document Editor Application with a Node.js WebSocket Server

This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.


ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.