Telerik blogs

RadWordsProcessing is a component that allows you to create, edit, import and export documents in different formats (plain text, RTF, docx and HTML). This blog post (1 of 2) aims to demonstrate the features of the library and introduce its API. It will show you how you can easily create or edit documents.

The current example will show you how to create a document that represents a standard job offer. The document will contain the company logo, a table with position identification, position description and a list with the required competencies. The final document will look like this:

Adding References

Before starting you should add the required assemblies to your project. The RadWordsProcessing library needs the following assemblies referenced:

  • Telerik.Windows.Documents.Core
  • Telerik.Windows.Documents.Flow
  • Telerik.Windows.Zip
  • PresentationCore
  • WindowsBase

Creating a Document

Now we are ready to build the example. Let’s start by creating our document and its editor. Then we can add a section and initialize the page size and margins. The editor will be used later for inserting the hyperlink:

RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
Section section = new Telerik.Windows.Documents.Flow.Model.Section(document);
section.PageMargins = new Telerik.Windows.Documents.Primitives.Padding(40, 10, 40, 10);
section.PageSize = PaperTypeConverter.ToSize(PaperTypes.A4);//WindowsBase assembly should be referenced.

Now we can create a standard page header. It will contain the page heading and the company logo. The page header is a separate block, which will contain a single paragraph. The header paragraph will contain the heading text, which will have specific font and size (text is hosted in run elements, these elements can be styled separately), and the logo image. 

Header defaultHeader = document.Sections.First().Headers.Add();
Paragraph defaultHeaderParagraph = defaultHeader.Blocks.AddParagraph();
defaultHeaderParagraph.TextAlignment = Alignment.Left;
Run title = defaultHeaderParagraph.Inlines.AddRun("Job Openings");
title.FontSize = 27;                 
//the PresentationCore assembly references required for the following properties.
title.ForegroundColor = new ThemableColor(System.Windows.Media.Colors.DarkBlue);
title.FontFamily = new ThemableFontFamily("Felix Titling");
FloatingImage logo = defaultHeaderParagraph.Inlines.AddFloatingImage();
using (MemoryStream ms = new MemoryStream())
    Image.FromFile(@"C:\logo.png").Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    logo.Image.ImageSource = new ImageSource(ms, "png");
logo.Image.Width = 200;
logo.Image.Height = 72;
logo.HorizontalPosition.ValueType = PositionValueType.Alignment;
logo.VerticalPosition.ValueType = PositionValueType.Alignment;
logo.VerticalPosition.Alignment = RelativeVerticalAlignment.Top;
logo.HorizontalPosition.Alignment = RelativeHorizontalAlignment.Right;

Then next step is creating the document list. We can set the properties for each list level. The level will be used later to connect the paragraph with the proper part of the document and each list element will be styled according to its level properties.

List list = new List();
list.Levels[0].StartIndex = 1;         
list.Levels[0].NumberingStyle = NumberingStyle.Decimal;
list.Levels[0].NumberTextFormat = 1 + ".";
list.Levels[1].StartIndex = 1;
list.Levels[1].NumberingStyle = NumberingStyle.Decimal;
list.Levels[1].NumberTextFormat = 2 + ".";
list.Levels[2].StartIndex = 1;
list.Levels[2].NumberingStyle = NumberingStyle.Decimal;
list.Levels[2].NumberTextFormat = 3 + ".";
list.Levels[3].NumberingStyle = NumberingStyle.None;
list.Levels[3].NumberTextFormat = "";
list.Levels[3].ParagraphProperties.LeftIndent.LocalValue = 48;
list.Levels[4].NumberingStyle = NumberingStyle.Bullet;
list.Levels[4].NumberTextFormat = "o";
list.Levels[4].ParagraphProperties.LeftIndent.LocalValue = 48;

The next part of the document is the Position Identification. This part contains a paragraph for the item text and a table. You can see how easy it is to add several elements using the built-in methods. Please note that the table is a block element and it should be added to the document section. The data for the table (along with all other strings) can be found at the end of the post.

Paragraph posIdentificationParagraph = section.Blocks.AddParagraph();
Run posIdentificationListItem = posIdentificationParagraph.Inlines.AddRun("Position Identification");
posIdentificationListItem.FontWeight = FontWeights.Bold;
posIdentificationParagraph.ListId = list.Id;
posIdentificationParagraph.ListLevel = 0;
Table posIdentificationTable = new Table(document);
posIdentificationTable.Indent = 60;
for (int i = 0; i < tableData.GetLength(0); i++)
    TableRow row = posIdentificationTable.Rows.AddTableRow();
    for (int j = 0; j < tableData.GetLength(1); j++)
        TableCell cell = row.Cells.AddTableCell();
        cell.Blocks.AddParagraph().Inlines.AddRun(tableData[i, j]);                 
        cell.PreferredWidth = new TableWidthUnit(200);

The position description contains two paragraphs. The first one is used for the list title. The second contains the actual job description. The document editor is used for inserting a hyperlink in the middle of the job description text.

Paragraph posDescriptionTitle = section.Blocks.AddParagraph();
var posDescriptionItem = posDescriptionTitle.Inlines.AddRun("Position description");
posDescriptionItem.FontWeight = FontWeights.Bold;
posDescriptionTitle.ListId = list.Id;       
posDescriptionTitle.ListLevel = 1;
Paragraph posDescriptionText = section.Blocks.AddParagraph();
var posDescriptionTextPart1 = posDescriptionText.Inlines.AddRun(desripionPart1);
editor.InsertHyperlink("Sitefinity CMS", "", false, "Sitefinity official site");
posDescriptionText.ListId = list.Id;
posDescriptionText.ListLevel = 3;

The final part of the document contains the list of the required competencies. As before the first paragraph contains the main list item text. The next 12 paragraphs contain the list items. They are added in a loop. 

Paragraph competenciesRequiredText = section.Blocks.AddParagraph();
Run competenciesRequiredItem = competenciesRequiredText.Inlines.AddRun("Competencies required");
competenciesRequiredItem.FontWeight = FontWeights.Bold;
competenciesRequiredText.ListId = list.Id;
competenciesRequiredText.ListLevel = 2;
for (int i = 0; i < competencies.Length; i++)
    Paragraph itemText = section.Blocks.AddParagraph();
    itemText.ListId = list.Id;
    itemText.ListLevel = 4;

The document structure is ready, and now we can export it. In this case, we will save it to a regular Microsoft Word file:

using (Stream output = new FileStream(@"..\..\output.docx", FileMode.Create))
    DocxFormatProvider provider = new DocxFormatProvider();
    provider.Export(document, output);

The next blog will demonstrate how you can use styles and add comments or bookmarks to this document.

The following data can be used for the example:

string desripionPart1 = @"Telerik is currently seeking a detail oriented Technical Support Officer to join our ";
string desripionPart2 = @" team in Sofia, Bulgaria. The Technical Support Officer will communicate directly with customers as well as serve as a technical resource for members of the sales team by addressing client’s complex technical inquiries. Relying on your background with ASP.NET / Web technologies and past experiences in client facing roles, your main duty will be to provide timely support to customers and members of the sales team over the phone, our online ticketing system, or email. You will need to analyze, track and resolve customer’s technical problems in a considerate and professional manner, and communicate closely with the appropriate development team. The ideal candidate will be able to quickly solve problems that involve a broad range of technical skills with an emphasis on the ASP.NET Framework and experience with CMS products such as Telerik’s Sitefinity ASP.NET CMS. You will be dealing mainly with developers and should be speaking with them on highly technical topics.";
string[] competencies = new string[]
    "Technical background in ASP.NET Microsoft platform (WebForms and MVC)",
    "Programming experience with LINQ, C#, and JavaScript",
    "Experience with HTML and CSS",
    "Experience with Networks, Hosting environments, IIS",
    "Experience with Microsoft SQL Server 2008/2012 is a plus",
    "Experience with ORM Framework is a plus",
    "Previous professional experience on a technical support related position",
    "Bachelor's degree in Computer Science or relevant field",
    "Exceptional problem-solving skills",
    "Exceptional ability to learn new skills",
    "Excellent communication skills: listening, written and verbal",
    "Strong work ethics and time management skills"
string[,] tableData = new string[,]
    { "Title", "Technical Support Officer" },
    { "Functional area", "Sitefinity CMS" },
    { "Reports to", "Sitefinity Team Leader" },
    { "Employment status", "Full-time job" }

We hope you found this blog useful. Be on the lookout for our second blog on the topic and learn how you can clone a document, change the styles for particular paragraphs, change the border of a table, and add bookmark and comment.


Happy coding!

Here are the projects we have created so far (C# and VB version)


About the Author

Dimitar Karamfilov

Dimitar Karamfilov is a Support Officer in the UI for WinForms team. He joined Telerik after graduating from the Telerik Academy in 2013. Apart from work he likes outdoor activities and reading philosophy literature.

Related Posts


Comments are disabled in preview mode.