Rich Text to Markdown

As a C# developer, you often face the need to convert rich text into various formats for different purposes. In this blog post, we’ll explore the user story of extracting rich text as Markdown syntax in C#, leveraging the strong Conholdate.Total for .NET library. By the end, you’ll have a clear understanding of the process and be ready to seamlessly integrate this feature into your projects

User Story:

As a C# developer working on a content management system, I want to empower users to input rich text and convert it into Markdown syntax. This will provide them with the flexibility and simplicity of Markdown formatting. Let’s look into the steps involved in achieving this goal using the Conholdate.Total for .NET library.

Step 1: Installing Conholdate.Total for .NET

To begin, acquire the Conholdate.Total for .NET library. Download it from the official Conholdate website or install it using a package manager like NuGet.

Install-Package Conholdate.Total 

Step 2: Initializing the Library

After installing the library, initialize it within your C# project. Configure the necessary settings and references to seamlessly integrate it with your existing codebase.

Step 3: Loading the Rich Text Document

Retrieve the rich text document that needs conversion to Markdown syntax. This document can be stored as a file, a database entry, or generated dynamically within your application. Conholdate.Total for .NET supports popular file formats such as DOCX, RTF, HTML, and more, facilitating compatibility with diverse rich text sources.

Step 4: Extracting Rich Text Content

Utilize Conholdate.Total for .NET to extract the content from the loaded rich text document. The library provides intuitive methods to access and manipulate various aspects of the document, such as paragraphs, headings, styles, and more.

To extract rich text from documents simply call the GetFormattedText method:

TextReader GetFormattedText(FormattedTextOptions options);

Step 5: Converting Rich Text to Markdown

Leveraging the capabilities of Conholdate.Total, convert the extracted rich text content to Markdown syntax. The library offers FormattedTextOptions class. The constructor of the class initializes it with the specified mode from the FormattedTextMode enum. You need to set FormattedTextMode.Markdown.

FormattedTextOptions(FormattedTextMode.Markdown)

Hence, here are the simple steps you need to follow:

  1. Firstly, instantiate a Parser object for the initial document.
  2. Next, instantiate a FormattedTextOptions object with Markdown text mode.
  3. Then, call the GetFormattedText method and obtain a TextReader object.
  4. Lastly, check if the reader isn’t null and read the text from the reader.

The source code will look like this:

// Create an instance of Parser class
using (Parser parser = new Parser("sample.docx"))
{
    // Extract a formatted text into the reader
    using (TextReader reader = parser.GetFormattedText(new FormattedTextOptions(FormattedTextMode.Markdown)))
    {
        // Print a formatted text from the document
        // If formatted text extraction isn't supported, a reader is null
        Console.WriteLine(reader == null ? "Formatted text extraction isn't supported" : reader.ReadToEnd());
    }
}

Step 6: Presenting the Converted Markdown

Once the rich text is successfully converted to Markdown, present the output to the user. This could involve providing a preview, saving the converted content to a file, or integrating it into a Markdown editor. Users can then further refine and enhance their content using familiar Markdown features.

Input:

Suppose the input file has the content like the following figure:

Rich Text to Markdown

Output:

The output will be:

**Rich Text**

Lorem ipsum **dolor sit amet**, consectetuer adipiscing elit. Maecenas porttitor congue massa. *Fusce posuere*, magna sed pulvinar ultricies, *purus lectus malesuada libero*, sit amet commodo magna eros quis urna.

1. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.
2. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
3.  Proin pharetra nonummy pede. Mauris et orci.

Aenean nec lorem

* In porttitor. Donec laoreet nonummy augue.
* Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc.
* Mauris eget neque at sem venenatis eleifend. Ut nonummy.

Summing Up

Congratulations! You have successfully completed the user story of extracting rich text as Markdown syntax in C# using the powerful Conholdate.Total for .NET library. By harnessing the library’s capabilities, you can effortlessly convert rich text documents into Markdown format, opening up new possibilities for content manipulation and formatting. Happy coding!

Conholdate.Total for .NET Resources:

Please refer to the following resources to further explore the library:

Get a Free License:

You can download a free temporary license to test the library without evaluation limitations.

Ask a Question:

You can post your questions or comments regarding the library in this forum.

See Also: