RadWordsProcessing enables you to open, create, modify and save flow documents and supports the most common flow document formats – DOCX, RTF, HTML and plain text. To utilize the abilities of the library you need to add three assemblies to your project:
Since we are talking about flow formats, no wonder the base class of the document model is RadFlowDocument. Let’s create one.
Let’s create one and associate it with our document. Further, to make sure our document is neatly aligned, let’s specify that all content added to the editor should be justified.
Now we are ready to start populating content. We can add a table to the document, place the text in one of its cells and the image in the other. Both of our table cells would need their own child paragraph elements, so we are going to add one in the first cell.
And in that first paragraph let’s say hello:
See how easy that was? The content editor would flow any content you want for you. Say you want to add a large paragraph of text. That’s not a problem – the InsertLine() method will add it for you and will break the line so you can immediately add to the next paragraph after that.
Let’s play around a little. Say you don’t want to break the current line, but you want to style separate words in your paragraph in different ways. That’s pretty easy too. The InsertLine() and InsertText() methods return the Run element they’ve added to the document. This allows you to directly modify the properties of the run. The next snippet will add some bold, italic, underlined, larger than the rest of the document and colorized text and will then complete the second paragraph.
Just like the InsertText() method returns the inserted run, the InsertParagraph() method returns the paragraph that was added. This allows you to make all sort of modifications, in this case I set the spacing-after of the paragraph saying “Regards” to 0.
At this point all desired text is in the first table cell, whereas the second cell is completely empty which is why it is not visible in the above pictures. Let’s add a paragraph that contains a picture in it.
In my project I’ve create a helper class that will obtain the picture as a resource stream. Here is what it does:
I can just use that stream and directly insert it through the content editor:
Our document is ready. All that is left is to export it.
Each of the file formats supported by RadWordsProcessing corresponds to a format provider class which enables you to easily import and export documents. Since we are exporting to HTML, we should create an HtmlFormatProvider instance.
The format provider exposes two overloads – one exports the document to a string and the other to the stream you pass to it. I’ll go ahead and use both. In this way my users will be able to view the generated HTML in a WebBrowser control:
And they will be able to save it at a destination of their choice:
I will just add a couple of buttons to the view that will execute the above code:
The code for the project is available in our SDK repository here. Go ahead and try it out. I also encourage you to reach out to us and tell us what you need from your document processing library.
Stay tuned for more goodies we are cooking for you.
Petya Sotirova is a Technical Support Manager for Telerik and Kendo UI. She joined the team as a WPF & Silverlight support engineer in 2012 and has been working on assisting customers and improving their experience ever since. Off work, you are most likely to find her curled up with a good book.
Subscribe to be the first to get our expert-written articles and tutorials for developers!