How to Convert Text to HTML in C#

In this how-to tutorial, we explain you step-by-step process to convert Text to HTML in C# by using one of the best document conversion package. You have to provide the plain Text file and it will be saved to the disk after performing conversion to the HTML format. You can also define many properties for customizing the converted HTML document as per your requirements. Further, this article provides a working code snippet to show you the working of convert Text to HTML C# functionality.

Steps to Convert Text to HTML in C#

  1. Install GroupDocs.Conversion for .NET package from the NuGet package manager in the .NET application to save Text as HTML using C#
  2. Add a reference to the GroupDocs.Conversion namespace for developing the Text to HTML functionality
  3. Initialize Converter class for loading the input Text document
  4. Set convert options for the output HTML document by initializing the MarkupConvertOptions class
  5. Finally, invoke the Convert method to generate HTML from Text and save it to the disk

We have defined above the stepwise instructions for configuring the required conversion library and how to create the C# convert Text to HTML application. The document conversion process can be initiated by creating an object of the Converter class that allows you to load the input text file after setting the required package and referencing the necessary namespaces. Then, you may define different parameters for customizing the converted HTML file by using the object of the MarkupConvertOptions class. The Convert method of the Converter class enables you to save the converted file to the disk.

Code to Convert Text to HTML in C#

using System;
using GroupDocs.Conversion.Options.Convert;
namespace ConvertTextToHtmlInCSharp
{
class Program
{
public static void Main(string[] args) // Main function to convert Text to HTML using C#
{
// Remove the watermark in output HTML document by adding license
string licensePath = "GroupDocs.Conversion.lic";
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License();
lic.SetLicense(licensePath);
// Load the source Text file for conversion to HTML
var converter = new GroupDocs.Conversion.Converter("sample.txt");
// Set the conversion options for HTML document to customize the output file
MarkupConvertOptions options = new MarkupConvertOptions();
// Convert and save the Text in HTML format
converter.Convert("converted.html", options);
Console.WriteLine("Done");
}
}
}

As you can see in the above code snippet, the convert plain Text to HTML C# capability is developed consuming the stepwise instructions defined in the previous section. Further, these instructions are platform-independent and can be used on any operating system without setting any additional software. Moreover, you can quickly transform a variety of document formats into HTML format including CSV, DOC, DOCX, DWG, EML, GIF, MD, and many more.

We have discussed the document conversion process to convert Text to HTML using C# and developed a sample code for it. Recently, we published an article to change PDF to RTF using C#, have a look at how to convert PDF to RTF in C# guide for more information.

 English