Looking to add functionality in your .NET apps to export data to Word and PDF? Learn four ways to do this with just a little code.
Building feature rich applications can often leave users asking to take visual elements off the screen to be used elsewhere. Being able to export from an application to Word or PDF opens up new possibilities, such as sharing, reporting or even satisfying Sarbanes–Oxley (SOX) audit requirements.
Getting data out of your application and into standard formats like Microsoft Word and Adobe PDF can be a daunting task. Especially when performance optimizations for large datasets, document customization & appearance, and supported platforms are taken into consideration.However, with the right tools, adding this feature to your .NET application can be as simple as a few lines of code. Let's take a look at 4 ways we can include rich document functionality in our .NET apps with very little code and time investment.
For the examples we'll be using the Telerik Document Processing Library (DPL), which is distributed at no additional cost with the Progress Telerik UI libraries for WPF, Silverlight and WinForms. For the web developers out there, the DPL is also included with ASP.NET Web Forms and MVC. In addition, no MS Office licenses or dependencies are required to use the DPL.
If you’re using Telerik UI, you already have these capabilities in your toolbox and we’ll learn how to leverage them. If you’re not using Telerik UI yet, but would like to increase your developer productivity, learn more about it here.
Having the ability to create a new Word document from code provides a great deal of flexibility. With a just few dependencies we have access to Word document creation APIs that include text, image, and link generation. For this example, we'll use
RadFlowDocumentEditor to create a new Word document from a .NET console application.
We'll start with a few dependencies like
System.IO to handle a file stream, and the necessary Telerik DPL assemblies.
Next, we create a new
RadFlowDocumentEditor will allow us to insert new text, image and other elements to the document.
Using the editor, we'll write some text to the document using the
InsertText method. We also have additional methods available such as:
Once the document is complete, we just need to write it to the
FileStream. For the
RadFlowDocument we'll use the
DocxFormatProvider to export to the DOCX format. Similarly, we could use a
PdfFormatProvider to create a PDF document.
In this example, files created using the Telerik DPL are saved directly to disk. However, any file created using the DPL can also be downloaded via the browser from UI for ASP.NET AJAX and UI for ASP.NET MVC applications.
Creating a mail merge is an effective way of building a template and populating it with data for processing documents in batches. With the mail merge process, you can create a series of personalized documents based on your data source.
The mail merge process is one that is often manually performed inside of Microsoft Word. With the Telerik DPL this process can be completely automated and customized based on the needs of your application. The mail merge APIs can be used virtually anywhere in .NET. To see it in action we'll use an example of how a mail merge is executed in a WPF/WinForms application.
We'll create a new document containing our template message. To create a template field in the document we'll use the
InsertField method and set the field code to
MERGEFIELD with the variable name we wish to use, in this case
FirstName. The variable name simply matches the property on an object that we'll supply to the
With the document created, the next step will be to perform the mail merge. The
MailMerge method takes any
IEnumerable as a parameter, in this case we'll use a predefined list of
GetRecipents. In this case
Person has a
LastName property matching the
The document can be saved by setting up a
MemoryStream and calling
Export from a
DocxFromatProvider. Once the memory stream is ready, we'll write it to a file.
The result is a multi-page .DOCX file for each recipient provided to the mail merge. This type of document can easily be printed and sent to clients as physical mail. See demos.telerik.com/wpf and the UI for WinForms demos for a more comprehensive example that includes additional file types and document features.
One of the Telerik DPL strengths is its ability to import and export various file types like: DOCX, PDF (export only), HTML and RTF. Because of this ability, it can also be used to do file conversions between these types as well. Using the
IFormatProvider we can utilize the various format providers within the Telerik DPL interchangeably. Let's take a look at a basic example of a console app that can import a DOCX file and export it to PDF.
We'll begin by creating a default provider using an
IFormatProvider and setting it to an instance of
RadFlowDocument is used as our document in the conversion from DOCX to PDF.
The DOCX document is opened from disk using the
DocxFormatProvider and streamed into memory.
With the document in memory as a
RadFlowDocument we can change our provider from a
PdfFormatProvider. Now the document can be written back to disk as a PDF.
As you can see in the complete example, in just a few short lines of code we're able to convert from DOCX to PDF.
This method of importing and exporting documents can be used to convert between file formats. One caveat however is that there is no import functionality for PDF documents due to file format limitations. To deal with this limitation the
IFormatProvider is equipped with a
CanImport property that can be checked before the
Import operation is performed.
Customers can sometimes ask for time-consuming project requirements, like the ability to edit Word Documents in the application without the need to leave the app and use external editors. On the surface this sounds like a feature that will take quite a bit of effort to accomplish; however, it's as simple as adding a rich text component to your application which comes with the Telerik UI for WPF and Telerik UI for WinForms suites.
Using UI for WPF and UI for WinForms, the ability to import and export right from the rich text editor control can be accomplished with few lines of code.
Exporting the content is just as simple. Simply pick the format provider for the desired format – PDF, DOCX, XAML, HTML, RTF, TXT and utilize its Export() method.
Whether you need to create documents from scratch or edit in the desktop app, the Telerik UI libraries make short work of any document export task for .NET developers. With the Telerik Document Processing Library new files can be created from scratch as DOCX, PDF, RTF, or TXT. Advanced features such as mail merge can reduce manual tasks with app driven automation. With the addition of Telerik UI libraries powered by UI for WPF and WinForms, documents can be edited on screen and custom export features empower users to export content with the click of a button.
Get access to the vast benefits of Telerik Document Processing and document exporting by downloading either the full Progress DevCraft bundle or any technology-specific library you need: Telerik UI for WPF or Telerik UI for WinForms.
Stefan Stefanov (MCPD) is a Senior Manager, Product Management and Product Marketing at Progress. He has been working with Telerik products since 2010, when he joined the company. Off work he enjoys traveling, hanging out with friends and reading various technology blogs. You can find Stefan on Twitter and LinkedIn.
Subscribe to be the first to get our expert-written articles and tutorials for developers!
All fields are required