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-platform 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). This includes parsing text, images, styles, fonts, tables, and other content.
  2. Lay out the document. This handles formatting such as page breaks, headers and footers, columns, and sections.
  3. Export to PDF.
The document is then rendered to a PDF recording graphics context, which captures the text layout and formatting data and saves it in a 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, add the NOV (Nevron Open Vision) NuGet package and then use the following code:

To convert an RTF file to PDF, use:
// 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:
NRichTextView richTextView = new NRichTextView();
richTextView.LoadFromLocalFile("c:\\temp\\RtfDocument.docx");
richTextView.SaveToLocalFile("c:\\temp\\PDFDocument.pdf");
								

How It Works

These are the steps that occur when you execute the preceding code:
  • The NRichTextView object is created. The view object hosts and arranges the document in memory.
  • LoadFromLocalFile loads the actual document into the view. You can also use LoadFromStream when the document is obtained from a non-file source, such as a blob field in a SQL database. During this process, the control reads the document's content and styling information, then processes the styling and applies the appropriate paragraph and table styles to each element.
  • SaveToLocalFile writes the PDF output. When writing to a paginated format such as PDF, the control lays out the text document content (paragraphs, tables, etc.) and then renders it in memory to a PDF content stream which is then serialized to a file. If you do not need to store the PDF to a file, you can also use SaveToStream, which will directly stream the resulting PDF to the passed stream. This can be very useful in HTTP handlers and other routines that are invoked from client-side HTML code to return dynamically generated PDF content that the user can directly view or download.

Customizing the Output

After loading the document into the rich text view, you can modify its contents before exporting it. For example, adding a header and changing the page size can be 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 intended visual representation is a complex task involving parsing, layout, and rendering. 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 specializing in the development of premium presentation layer solutions for .NET-based technologies. Today, Nevron has established itself as a trusted partner worldwide for .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.
  
The quality of the graphics we produce with Nevron Chart greatly impresses everyone. While the software development department in our startup company consists of only five people, the fact that we can produce world-class graphics goes a long way towards convincing potential clients of our professionalism.

I like to lead off the demonstrations that I provide for customers with one or more of the plots I generate with Nevron Chart. The quality of these graphics is so obvious and so arresting that they grab everyone's attention, and make sure that the people in the room are going to listen to whatever I have to say next.

The Nevron Chart software is absolutely central to our software development efforts. The data analysis package that we've written uses graphics to tell the stories that lies behind the data, and the precision and user interactivity that we can achieve with the Nevron package enable us to tell those stories in a very compelling fashion.

We have been using Nevron Chart as a central element in our software development efforts for the last three years, and we have no desire to switch to any other package. I've been consistently impressed with the robustness of the code, the range of the capabilities, and above all with the quality of the graphics since the time that I first started experimenting with this package. Graphical presentations have been a part of my scientific programming efforts for the last 20 years, and as far as I'm concerned there is no other package on the market that is better than Nevron Chart.
  

Benjamin Alexander
BioProcessors