TX Text Control .NET for Windows Forms Documentation

Sample 4: Converting Documents

This sample shows two interesting things: How to upload a document to the server and how to convert this document to another supported document format. This sample consists of a file input form and a radio button list that is used to provide the user the document format selection. The screenshot below shows the sample's web form:

After choosing a file to upload, a file format must be selected. On clicking the button 'Convert Document', the file is uploaded to the server and converted using TX Text Control .NET Server. Possible formats to upload are: MS Word, RTF and ANSI Text. These documents can be converted to the supported file formats PDF, MS Word, RTF and HTML.

The file is uploaded using an HTML file field, whose content can be requested server-side and read in into a Byte array:

[C#] int iLength = (int)File1.PostedFile.InputStream.Length; byte[] fileStream = new byte[iLength]; File1.PostedFile.InputStream.Read(fileStream, 0, iLength);
[Visual Basic] Dim fileStream As Byte() Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer) File1.PostedFile.InputStream.Read(fileStream, 0, iLength)

After checking the content type of the posted file, the document is loaded with the appropriate stream type using the Load method:

[C#] switch(File1.PostedFile.ContentType) { case "application/msword": if(File1.PostedFile.FileName.EndsWith("doc")) serverTextControl1.Load(fileStream, TXTextControl.BinaryStreamType.MSWord); else if(File1.PostedFile.FileName.EndsWith("rtf")) serverTextControl1.Load(Convert.ToBase64String(fileStream), TXTextControl.StringStreamType.RichTextFormat); else return; break; case "text/html": if(File1.PostedFile.FileName.EndsWith("htm") || File1.PostedFile.FileName.EndsWith("html")) serverTextControl1.Load(Convert.ToBase64String(fileStream), TXTextControl.StringStreamType.HTMLFormat); else return; break; case "text/plain": serverTextControl1.Load(Convert.ToBase64String(fileStream), TXTextControl.StreamType.PlainAnsiText); break; default: return; }
[Visual Basic] Select Case File1.PostedFile.ContentType Case "application/msword" if File1.PostedFile.FileName.EndsWith("doc") then ServerTextControl1.Load(fileStream, TXTextControl.StreamType.MSWord) ElseIf File1.PostedFile.FileName.EndsWith("rtf") then ServerTextControl1.Load(fileStream, TXTextControl.StreamType.RichTextFormat) else Exit Sub End if Case "text/html" if File1.PostedFile.FileName.EndsWith("htm") Or File1.PostedFile.FileName.EndsWith("html") then ServerTextControl1.Load(fileStream, TXTextControl.StreamType.HTMLFormat) else Exit Sub End if Case "text/plain" ServerTextControl1.Load(fileStream, TXTextControl.StreamType.PlainAnsiText) Case else Exit Sub End Select

Finally, the loaded document is saved in the selected format using the Save method of TX Text Control .NET Server.

 
 
 

Products

Support

Downloads

Corporate

Buy Now