Converting Word Documents to PDF with Nevron Rich Text Editor for .NET

Introduction

The Nevron Rich Text Editor for .NET is a powerful and feature-rich component that enables developers to integrate professional-grade word processing functionality into their Windows Forms, WPF, Blazor, and ASP.NET Core applications. One of its standout features is the ability to load Word documents in DOCX or RTF formats and export them to PDF—a capability supported across all application types, including desktop (WinForms/WPF), ASP.NET Core, and Blazor.
This article outlines how to create a simple routine that converts Word documents to PDF using the API provided by the control.

Text Formats Overview

Before diving into the implementation, let's briefly examine the input and output formats involved in the conversion process.

RTF (Rich Text Format)
Introduced in the 1980s, RTF was the original rich text format used by Microsoft Word. It represents formatting using tags, optionally grouped in groups to define the effect of their scope. RTF was designed to be forward and backward compatible, allowing older readers to ignore unfamiliar tags and newer readers to process older files seamlessly. Notable features also include embedded fonts for cross-platform readability, support for WMF/EMF, and others.
Pros:
  • Cross platfrom support
  • Forward and backward compatible
  • Text based
Cons:
  • Bulky file size
  • Lack of Unicode support in early versions
  • Not human-readable
DOCX (Office Open XML)
DOCX is the modern Word document format, part of the Office Open XML standard. Unlike the older .doc format, DOCX uses XML to structure content. This format is more modular and supports advanced features such as tracked changes and modern styling.

Pros:
  • Better structured than RTF
  • Supports advanced Word features (track changes, shapes, charts, etc.)
Cons:
  • Larger file size due to XML
  • Not human-readable
PDF (Portable Document Format)
Developed by Adobe in the 1990s, PDF aims to preserve document layout and formatting across platforms. Unlike RTF or DOCX, which store content and styling, PDF also stores the layout instructions, ensuring documents look the same everywhere. Pros:
  • Fixed, consistent presentation
  • Cross-platform compatibility
Cons:
  • Not easily editable
  • Less suitable for rich text editing or dynamic updates

Text Conversion Process Overview

Converting RTF or DOCX to PDF involves three major steps:
  1. >Read the source document (RTF/DOCX)
  2. This includes parsing text, images, styles, fonts, tables, and other content.
  3. Layout the document

  4. Handles formatting like page breaks, headers/footers, columns, and sections.
  5. Export to PDF
The document is then painted on a PDF recording graphics which captures the text layout and formatting data and saves it in PDF-compliant format. While this is a very complex operation internally, the Nevron Rich Text Editor for .NET simplifies the entire process to just a few lines of code.

To get started, initialize NOV (Nevron Open Vision) and use the Rich Text component:

NNovApplicationInstaller.Install(Nevron.Nov.Text.NTextModule.Instance);

// Perform the actual conversion
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile("c:\\temp\\RtfDocument.rtf");
richTextView.SaveToLocalFile("c:\\temp\\PDFDocument.pdf");
To convert a DOCX file, the process is identical:
NNovApplicationInstaller.Install(Nevron.Nov.Text.NTextModule.Instance);
									
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile("c:\\temp\\RtfDocument.docx");
richTextView.SaveToLocalFile("c:\\temp\\PDFDocument.pdf");

How It Works

Those are the steps that happen when you execute the above lines of code:
  • The NRichTextView object is created. It hosts and arranges the document in memory.
  • LoadFromLocalFile loads the document into memory. You can also use LoadFromStream in cases when the document is obtained from non-file sources (for example a blob field in a SQL database).
  • During this process, the control will read the content and styling information in the document. Then it will process the styling and apply proper paragraph and table styles, to each element in the document.
  • SaveToLocalFile writes the PDF output. Similarly, SaveToStream is available for stream-based output.
  • Before saving the control will arrange the document. This process involves measuring the paragraphs, and table cells to determine their best size and then computing the document flow. In this process the control will regard the indents and spacings specified in the initial document as well as pagination instructions. Finally, the control will render the document to the PDF output media.

Customizing the Output

After you load the document in the rich text view you can modify its contents before exporting. For example, adding a header and changing the page size is achieved with the following code:
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile("c:\\temp\\RtfDocument.rtf");

NHeaderFooter header = new NHeaderFooter();
header.Blocks.Add(new NParagraph("Performing automatic conversion from RTF/DOCX to PDF using C#"));

richTextView.Content.Sections[0].Header = header;
richTextView.Content.Sections[0].PageSize = new NPaperSize(ENPaperKind.A4Plus);

richTextView.SaveToLocalFile("c:\\temp\\PDFDocument.pdf");

Conclusion

Converting rich text documents (RTF/DOCX) to a format like PDF that preserves the exact visual representation is a complex task involving parsing, layout, and rendering. The Nevron Rich Text Editor for .NET abstracts this complexity and allows you to accomplish the task with minimal code. Moreover, it offers powerful APIs for programmatically editing and customizing documents prior to export—making it suitable not only for conversion but also for document automation, formatting, indexing, and more.

About Nevron Software

Founded in 1998, Nevron Software is a component vendor specialized in the development of premium presentation layer solutions for .NET based technologies. Today Nevron has established itself as the trusted partner worldwide for use in .NET LOB applications, SharePoint portals and Reporting solutions. Nevron technology is used by many Fortune 500 companies, large financial institutions, global IT consultancies, academic institutions, governments and non-profits.
For more information, visit: www.nevron.com.
  
We started a search for new components as we switch away from VB/C++ development to .Net, and felt it was time to re-evaluate all controls we commonly use.

I started the evaluation by looking at all charting controls available on Component Source. (The product we currently have used is [by another leading vendor]).

The initial evaluation took the form of creating a simple x-y chart with a x-axis with a lot of times along it, and passing in random y values, and then formatting the chart to look reasonable. This is a good test of ease of use, documentation, examples provided etc. The C# samples provided were excellent!

This amazingly reduced the choice down to just three. The next step was to look at cost. You had a sensible pricing policy, which is where [the other leading vendor] lost out on it's deployment costs. We wanted a WinForms solution, but with a possibility of Web Forms too, without having to go through another big investment.

The final points that swung it for us were...
- Great looks.
- A company that makes and concentrates on a few good products, rather than tens of mediocre ones.
- A good website, where you can get the info you want.
  

Miles Dennis
CACI Ltd