Telerik blogs
With this second blog post on WordProcessing in UI for WinForms, we want to demonstrate how you can load and edit the document we created in the previous blog. We will look into how we can clone the document, change the styles for particular paragraphs, change the border of a table, and add bookmark and comment. After we are finished, the document should look like this:
Figure 1. The document after the changes are introduced.

First, we can start by loading the document and creating a cloned copy that can be used for testing purposes. (This way you will be able to observe the result at any point without affecting the actual document). In addition, we can create a document editor, which we will use later to navigate in the document.

RadFlowDocument source;
using (FileStream fs = new FileStream(@"C:\output.docx", FileMode.Open))
{
    DocxFormatProvider provider = new DocxFormatProvider();
    source = provider.Import(fs);
}
 
RadFlowDocument document = new RadFlowDocument();
document = source.Clone();      
 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);

We are ready to create the paragraph and table styles. For the paragraph style we will set the fore color and the font. This style will be used for the main list headers. 

Style paragaphStyle = new Style("CompetenciesRequired", StyleType.Paragraph);
paragaphStyle.Name = "CompetenciesRequiredStyle";
 
paragaphStyle.CharacterProperties.FontFamily.LocalValue = new ThemableFontFamily("Constantia");
paragaphStyle.CharacterProperties.FontSize.LocalValue = 22;
 
paragaphStyle.CharacterProperties.ForegroundColor.LocalValue = new ThemableColor(Colors.DarkSlateBlue);
 
document.StyleRepository.Add(paragaphStyle);
 
foreach (Paragraph paragraph in document.Sections[0].EnumerateChildrenOfType<Paragraph>())
{
    if (paragraph.ListLevel < 3)
    {
        paragraph.StyleId = paragaphStyle.Id;
    }
}

 


We can create a style for the table as well. For this example we will just set different border style and border color.

Style tableStyle = new Style("TableStyle", StyleType.Table);
tableStyle.Name = "Table Style";
Border tableBorder = new Border(2, Telerik.Windows.Documents.Flow.Model.Styles.BorderStyle.Wave, new ThemableColor(Colors.DarkSlateGray));
tableStyle.TableProperties.Borders.LocalValue = new TableBorders(tableBorder);
 
document.StyleRepository.Add(tableStyle);
 
foreach (Table table in document.Sections[0].EnumerateChildrenOfType<Table>())
{
    table.StyleId = tableStyle.Id;
}

 



In the next step, we will iterate through all document blocks and insert a comment and a bookmark in the document. We will use the document editor to navigate in the paragraphs. In this example, those elements will be added to the competencies required list.

foreach (var item in document.Sections[0].Blocks)
{
    var paragraph = item as Paragraph;
    if (paragraph != null && paragraph.Inlines.Count > 0)
    {
        if (paragraph.Inlines[0] is Run)
        {
            if (((Run)paragraph.Inlines[0]).Text.Contains("ASP.NET") && paragraph.ListLevel == 4)
            {
                editor.MoveToParagraphStart(paragraph);
                editor.MoveToInlineEnd(paragraph.Inlines[0]);
                editor.InsertComment("At least 2 years of experience is required", paragraph.Inlines[0], paragraph.Inlines[0]);
            }
            else if (((Run)paragraph.Inlines[0]).Text.Contains("HTML") && paragraph.ListLevel == 4)
            {
                editor.MoveToParagraphStart(paragraph);
                 
                Bookmark bookmark = new Bookmark(document, "HTML");
                 
                paragraph.Inlines.Insert(0, bookmark.BookmarkRangeStart);
                paragraph.Inlines.Add(bookmark.BookmarkRangeEnd);
            }
        }
    }
}

 



We hope that these two blogs will get you started with the WordProcessing libraries of Telerik UI for WinForms. 

Feel free to give them a try here.

Following you can find projects in C# and VB summarizing all the work we have done so far in this blog post.

Happy coding



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

Comments are disabled in preview mode.