How to convert a Word DOCX Document to Text TXT in C# .NET Framework
File format conversion, everybody’s favorite subject, right? Maybe we can make things a little easier today.
First we are going to need to install our API client so we can get things rolling. That just means running this snippet in the Package Manager console.
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0
Ok, now we are going to invoke ConvertDocumentDocxToTxt, as you see here, and aim it at our DOCX file to be converted.
using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ConvertDocumentDocxToTxtExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ConvertDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.var textFormattingMode = textFormattingMode_example; // string | Optional; specify how whitespace should be handled when converting the document to text. Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases. Default is 'minimizeWhitespace'. (optional)try{// Convert Word DOCX Document to Text (txt)TextConversionResult result = apiInstance.ConvertDocumentDocxToTxt(inputFile, textFormattingMode);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentDocxToTxt: " + e.Message );}}}}
And that’s it! Our API is going to do all of the annoying grunt work for us and return our completed TXT file. We have a ton more useful APIs like this, and they provide functionality for everything from OCR to barcodes.