Rich Text to Markdown

As a C# developer, you often need to convert rich text into different formats. In this post, we explore extracting rich text as Markdown syntax in C# with the Conholdate.Total for .NET library. By the end, you’ll understand the process and be ready to add this feature to your projects.

User Story:

As a C# developer working on a content management system, I want users to input rich text and convert it into Markdown syntax. This provides the flexibility and simplicity of Markdown formatting. Below are the steps to achieve this using Conholdate.Total for .NET.

Step 1: Installing Conholdate.Total for .NET

Acquire the Conholdate.Total for .NET library. Download it from the official Conholdate website or install it via NuGet.

Install-Package Conholdate.Total 

Step 2: Initializing the Library

After installing, initialize the library in your C# project. Configure the required settings and references to integrate it with your codebase.

Step 3: Loading the Rich Text Document

Load the rich text document you want to convert. It can be a file, a database entry, or generated dynamically. Conholdate.Total for .NET supports popular file formats such as DOCX, RTF, HTML, and more.

Step 4: Extracting Rich Text Content

Use Conholdate.Total to extract content from the loaded document. The library offers methods to access paragraphs, headings, styles, and other elements.

To extract rich text, call the GetFormattedText method:

TextReader GetFormattedText(FormattedTextOptions options);

Step 5: Converting Rich Text to Markdown

Convert the extracted content to Markdown using the FormattedTextOptions class. Set the mode to FormattedTextMode.Markdown.

FormattedTextOptions(FormattedTextMode.Markdown)

Follow these steps:

  1. Create a Parser object for the source document.
  2. Instantiate a FormattedTextOptions object with Markdown mode.
  3. Call the GetFormattedText method to obtain a TextReader object.
  4. If the reader is not null, read the text.

Example code:

// Create an instance of Parser class
using (Parser parser = new Parser("sample.docx"))
{
    // Extract formatted text into the reader
    using (TextReader reader = parser.GetFormattedText(new FormattedTextOptions(FormattedTextMode.Markdown)))
    {
        // Output the formatted text or indicate unsupported extraction
        Console.WriteLine(reader == null ? "Formatted text extraction isn't supported" : reader.ReadToEnd());
    }
}

Step 6: Presenting the Converted Markdown

After conversion, display the Markdown to the user. Options include a preview, saving to a file, or loading it into a Markdown editor for further editing.

Input:

Suppose the input file contains the following content:

Rich Text to Markdown

Output:

The resulting Markdown 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 completed the user story of extracting rich text as Markdown syntax in C# using Conholdate.Total for .NET. The library makes it easy to convert rich text documents into Markdown, opening 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:

Post your questions or comments about the library in this forum.

See Also: