Converting DOCX Documents to PDF with NOV Rich Text Editor for .NET

Introduction

DOCX is the most widely used text format. It is the native format of MS Word and is also accepted as a persistency format by all other word processing applications, because it is the de facto standard for document exchange.

Like HTML, DOCX allows documents to be edited and reflowed, but differs fundamentally in the areas that define how pagination is performed and also in styling. Therefore, typical browser-based converters cannot properly handle DOCX to PDF conversion without additional modifications.

PDF, on the other hand, is a paginated format which cannot be reflowed - you can think of it as a vectorized copy of a reflowed document. Because PDF features font embedding, it is also very portable because all the information needed to present the document is embedded inside the document itself.

From DOCX Content to PDF Pages

The process of converting an XML-based format like DOCX to PDF involves several stages, all of which are implemented by NOV Rich Editor for .NET.

The first step is to parse the passed XML and convert it to the NOV Rich Text Editor for .NET DOM (Document Object Model).

Next, the document needs to be styled, measured and laid out page by page.

Finally, internally, the document must be rendered to PDF output graphics, which record all calls to the graphics subsystem that otherwise result in content being displayed on the screen and direct them to PDF output.

Finally, the captured PDF output is output to a file or a stream.

Fortunately, all those steps are performed internally, and the code required to perform this is pretty minimal.

Convert a DOCX File to PDF

using Nevron.Nov.Text;
									/// <summary>
									/// Converts an existing DOCX document to a PDF file.
									/// NOV must be initialized before this method is called.
									/// </summary>
									/// <param name="sourcePath">The full path to the source DOCX file.</param>
									/// <param name="targetPath">The full path to the destination PDF file.</param>
									public static void ConvertDocxToPdf(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.
									ConvertDocxToPdf(@"C:\Documents\Source.docx", @"C:\Documents\Source.pdf");
								

LoadFromLocalFile reads the DOCX document into an NRichTextView. SaveToLocalFile exports the loaded document to the specified PDF path.

Customize the Document Before Export

Once the DOCX document has been loaded, its content is accessible through the NOV Rich Text DOM. The following example replaces the header of the first section before exporting it. This can be useful when adding an internal document label or marking the document as processed (a common example of this is the paid stamp placed on invoices).

									NRichTextView richTextView = new NRichTextView();
									richTextView.LoadFromLocalFile(@"C:\Documents\Source.docx");

									// Replace the first section's header when a section is present.
									if (richTextView.Content.Sections.Count > 0)
									{
									NHeaderFooter header = new NHeaderFooter();
									header.Blocks.Add(new NParagraph("Internal document copy"));
									richTextView.Content.Sections[0].Header = header;
									}

									richTextView.SaveToLocalFile(@"C:\Documents\Source.pdf");
								

Check the Generated PDF

As mentioned above, the PDF format contains all the information needed to render the document on another system. This is the primary reason why it is so largely accepted.

To ensure that the conversion process is correct, you must ensure that the system performing the conversion has all fonts installed. For example, on Windows systems, fonts are stored in the C:\Windows\Fonts folder. Alternatively, you can distribute those fonts alongside your application or service and provide them to the NOV font subsystem using:

									// Installing a font from memory
									NApplication.FontService.InstalledFontsMap.InstallFont(new NOTMemoryInstalledFont(someByteArray));

									// Installing a font from a file
									NApplication.FontService.InstalledFontsMap.InstallFont(new NOTFileInstalledFont(@"c:\windows\fonts\arimo.ttf"));

									// Installing a font from a resource
									NApplication.FontService.InstalledFontsMap.InstallFont(new NOTResourceInstalledFont(someEmbeddedResource));
								

This allows you to ensure that, like PDF, your conversion routine is portable as well. Note that you must check the font redistribution rights before doing so, as some fonts do not allow redistribution and are bundled with the OS.

Naturally, as with all conversion processes, you need to inspect the result to ensure that the document is properly converted to PDF.

Conclusion

NOV Rich Text Editor for .NET provides a direct and easy-to-use way for C# applications or services to convert DOCX documents to PDF. The same API can be used in desktop, server and web applications (Blazor), as the control is cross-platform. In addition, the control allows applications to embed fonts that will be used in the conversion, which makes it very suitable for server-side environments where it is common for code developed on Windows to be executed in a Linux-hosted Docker container, for example.

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.

Customer Quotes

  
I really like the fact that standard windows icons are provided with the NOV Ribbon example, avoiding the need to recreate the wheel and giving a more professional appearance. Using the provided Visual Studio NOV solution, it is fairly straightforward to add custom icons with a few extra simple steps. The gallery of C# examples for all controls makes it easy to create custom layouts. For the handful of customizations I could not find in the examples or user forums, the tech support responses got me going within a day.
  

Jay Wilson
K2realm, LLC