The RTF format is very old - in fact, its roots trace back to 1987, when the first mention of RTF appeared in Microsoft Systems Journal.
At its core, the format contains groups, and each group contains tags (control words). When constructing the final document, the reader must process the groups/tags it understands and construct the document based on the structure of the incoming RTF.
Separately, the importer needs to maintain several states - character, paragraph, table, table cell, etc. - which apply when the importer processes different parts of the incoming document.
Overall, the RTF format was a big mess from the beginning, which was further complicated by all the modifications to the standard up to 2008, when the final RTF 1.9.1 specification was released.
To its credit, RTF is a forward- and backward-compatible text format, meaning older readers can read output from newer writers and vice versa. This is the main reason why it is so widely used to date - applications from different platforms, versions, etc., can communicate using RTF and exchange formatted text - this is why every decent text editor to date must accept RTF input from the clipboard.
Nevron Rich Text Editor for .NET supports a very large part of the RTF specification - it understands 700+ tags and control words, making this a very high-quality importer comparable to MS Word.
The conversion process is similar to the one used for DOCX:
As mentioned above, this process is internally very complicated, as the control has to take into account the numerous tags of the RTF specification. One important thing to mention here is that Nevron Rich Text Editor is extremely fast when importing RTF, which makes it suitable for systems performing large-volume RTF conversion or indexing, for example.
Now that we've covered the internal steps performed by the control, let's take a look at what the conversion procedure looks like:
/// <summary>
/// Converts an existing RTF document to a PDF file.
/// NOV must be initialized before this method is called.
/// </summary>
/// <param name="sourcePath">The full path to the source RTF file.</param>
/// <param name="targetPath">The full path to the destination PDF file.</param>
public static void ConvertRtfToPdf(string sourcePath, string targetPath)
{
// Load the source document into the rich text view.
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile(sourcePath);
// The destination file extension selects PDF output.
richTextView.SaveToLocalFile(targetPath);
}
// Example call after application initialization.
ConvertRtfToPdf(@"C:\Documents\Source.rtf", @"C:\Documents\Source.pdf");
LoadFromLocalFile loads the RTF document into an NRichTextView. SaveToLocalFile exports the loaded document to the specified PDF path. This is practically the same code as the one used in the DOCX-to-PDF and HTML-to-PDF conversions. One of the strong points of the Nevron Rich Text Editor is that it provides a consistent API that shields the developer from the complexities of the format, so all you have to do is execute two lines of code.
Further, imported RTF content can be modified before PDF export. For example, an application can append a processing note to the final section:
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile(@"C:\Documents\Source.rtf");
// Append a note to the final section when a section is present.
int sectionCount = richTextView.Content.Sections.Count;
if (sectionCount > 0)
{
NSection section = richTextView.Content.Sections[sectionCount - 1];
section.Blocks.Add(new NParagraph("Processed by the document service."));
}
richTextView.SaveToLocalFile(@"C:\Documents\Source.pdf");
NOV Rich Text Editor for .NET provides high-fidelity RTF import that implements very large parts of the RTF specification by covering all essential parts like character and paragraph formatting, tables, headers and footers, sections, bookmarks, etc. This allows you to use plain C# to convert RTF documents to PDF. The conversion can also include automated modifications because the imported RTF is available for editing through the editor's DOM. The same document API can be incorporated into desktop applications and automated document services.