RichTextBox for UWP | ComponentOne
Working with C1Document Object / Creating Documents and Reports
In This Topic
    Creating Documents and Reports
    In This Topic

    To illustrate the process of creating a C1Document, we will walk through the steps required to implement a simple assembly documentation utility.

    To start, create a new project and add a reference to the C1.UWP and C1.UWP.RichTextBox assemblies. Then edit the page constructor as follows:

    C#
    Copy Code
    using C1.Xaml;
    using C1.Xaml.RichTextBox;
    using C1.Xaml.RichTextBox.Documents;
    using System.Reflection;
    using System.Text;
    using Windows.UI;
    using Windows.UI.Text;
    
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace RTBTestsDoc
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            // C1RichTextBox that will display the C1Document
            C1RichTextBox _rtb;
    
            public MainPage()
            {
                this.InitializeComponent();
                // Create the C1RichTextBox and add it to the page
                _rtb = new C1RichTextBox();
                LayoutRoot.Children.Add(_rtb);
    
             // Create document and show it in the C1RichTextBox
                _rtb.Document = DocumentAssembly(typeof(C1RichTextBox).GetTypeInfo().Assembly);
             _rtb.IsReadOnly = true;
    
            }
    

    The code creates the C1RichTextBox and assigns its C1RichTextBox.Document property to the result of a call to the DocumentAssembly method. It then makes the control read-only so users can't change the report.

    The DocumentAssembly method takes an Assembly as argument and builds a C1Document containing the assembly documentation. Here is the implementation:

    C#
    Copy Code
    C1Document DocumentAssembly(Assembly asm)
            {
                // Create document
                C1Document doc = new C1Document();
                doc.FontFamily = new FontFamily("Tahoma");
                //Heading1 H = new Heading1("Assembly\r\n" + asm.FullName.Split(',')[0]);
                // Assembly
                Heading1 p = new Heading1();
                p.Inlines.Add(new C1Run() { Text = "Assembly\r\n" + asm.FullName.Split(',')[0] });
                doc.Blocks.Add(p);
    
                // Types
                foreach (Type t in asm.ExportedTypes)          
                    DocumentType(doc, t);
             
                // Done
                return doc;
            }
    

     The method starts by creating a new C1Document object and setting its C1TextElement.FontFamily property. This will be the default value for all text elements added to the document.

    Next, the method adds a Heading1 paragraph containing the assembly name to the new document's Blocks collection. Blocks are elements such as paragraphs and list items that flow down the document. They are similar to "div" elements in HTML. Some document elements contain an Inlines collection instead. These collections contain elements that flow horizontally, similar to "span" elements in HTML.

    The Heading1 class inherits from C1Paragraph and adds some formatting. We will add several such classes to the project, for normal paragraphs and headings 1 through 4.

    The Normal paragraph is a C1Paragraph that takes a content string in its constructor:

    C#
    Copy Code
    class Normal : C1Paragraph
            {
                public Normal()
                {
                   // this.Inlines.Add(new C1Run() { Text = text });
                    this.Padding = new Thickness(30, 0, 0, 0);
                    this.Margin = new Thickness(0);
                }
            }
    

    The Heading paragraph extends Normal and makes the text bold:

    C#
    Copy Code
    class Heading : Normal
            {
                public Heading()
                {
                    this.FontWeight = FontWeights.Bold;
                }
            }
    

    Heading1 through Heading4 extend Heading to specify font sizes, padding, borders, and colors:

    C#
    Copy Code
    class Heading1 : Heading
            {
                public Heading1()
                {
                    this.Background = new SolidColorBrush(Colors.Yellow);
                    this.FontSize = 24;
                    this.Padding = new Thickness(0, 10, 0, 10);
                    this.BorderBrush = new SolidColorBrush(Colors.Black);
                    this.BorderThickness = new Thickness(3, 1, 1, 0);
                }
            }
            class Heading2 : Heading
            {
                public Heading2()
                {
                    this.FontSize = 18;
                    this.FontStyle = FontStyle.Italic ;
                    this.Background = new SolidColorBrush(Colors.Yellow);
                    this.Padding = new Thickness(10, 5, 0, 5);
                    this.BorderBrush = new SolidColorBrush(Colors.Black);
                    this.BorderThickness = new Thickness(3, 1, 1, 1);
                }
            }
            class Heading3 : Heading
            {
                public Heading3()
                {
                    this.FontSize = 14;
                    this.Background = new SolidColorBrush(Colors.LightGray);
                    this.Padding = new Thickness(20, 3, 0, 0);
                }
            }
            class Heading4 : Heading
            {
                public Heading4()
                {
                    this.FontSize = 14;
                    this.Padding = new Thickness(30, 0, 0, 0);
                }
            }
    

    Now that we have classes for all paragraph types in the document, it's time to add the content. Recall that we used a DocumentType method in the first code block. Here is the implementation for that method:

    C#
    Copy Code
    void DocumentType(C1Document doc, Type t)
            {        
    
                // Type
                Heading2 h2 = new Heading2();
                h2.Inlines.Add(new C1Run() { Text = "Class " + t.Name });
                doc.Blocks.Add(h2);
    
                // Properties
                Heading3 h3 = new Heading3();
                h3.Inlines.Add(new C1Run() { Text = "Properties" });
                doc.Blocks.Add(h3);
                foreach (PropertyInfo pi in t.GetRuntimeProperties())
                {
                    if (pi.DeclaringType == t)
                        DocumentProperty(doc, pi);
                }
    
                // Methods
                h3 = new Heading3();
                h3.Inlines.Add(new C1Run() { Text = "Methods" });
                doc.Blocks.Add(h3);
                foreach (MethodInfo mi in t.GetRuntimeMethods())
                {
                    if (mi.DeclaringType == t)
                        DocumentMethod(doc, mi);
                }
    
                // Events
                h3 = new Heading3();
                h3.Inlines.Add(new C1Run() { Text = "Events" });
                doc.Blocks.Add(h3);
                foreach (EventInfo ei in t.GetRuntimeEvents())
                {
                    if (ei.DeclaringType == t)
                        DocumentEvent(doc, ei);
                }
            }
    

    The method adds a Heading2 paragraph with the class name and then uses reflection to enumerate all the public properties, events, and methods in the type. The code for these methods is simple:

    C#
    Copy Code
    void DocumentProperty(C1Document doc, PropertyInfo pi)
            {
                if (pi.PropertyType.IsGenericParameter)
                    return;
    
                Heading4 h4 = new Heading4();
                h4.Inlines.Add(new C1Run() { Text = pi.Name });
                doc.Blocks.Add(h4);
    
                var text = string.Format("public {0} {1} {{ {2}{3} }}",
                  pi.PropertyType.Name,
                  pi.Name,
                  pi.CanRead ? "get; " : string.Empty,
                  pi.CanWrite ? "set; " : string.Empty);
                Normal n = new Normal();
                n.Inlines.Add(new C1Run() { Text = text });
                doc.Blocks.Add(n);
            }
    

    The method adds a Heading4 paragraph containing the property name, then some Normal text containing the property type, name, and accessors.

    The methods used for documenting events and properties are analogous:

    C#
    Copy Code
    void DocumentMethod(C1Document doc, MethodInfo mi)
            {
                if (mi.IsSpecialName)
                    return;
    
                Heading4 h4 = new Heading4();
                h4.Inlines.Add(new C1Run() { Text = mi.Name });
                doc.Blocks.Add(h4);
                var parms = new StringBuilder();
                foreach (var parm in mi.GetParameters())
                {
                    if (parms.Length > 0)
                        parms.Append(", ");
                    parms.AppendFormat("{0} {1}", parm.ParameterType.Name, parm.Name);
                }
                var text = string.Format("public {0} {1}({2})",
                  mi.ReturnType.Name,
                  mi.Name,
                  parms.ToString());
    
                Normal n = new Normal();
                n.Inlines.Add(new C1Run() { Text = text });
                doc.Blocks.Add(n);
            }
    
            void DocumentEvent(C1Document doc, EventInfo ei)
            {
                Heading4 h4 = new Heading4();
                h4.Inlines.Add(new C1Run() { Text = ei.Name });
                doc.Blocks.Add(h4);
    
                var text = string.Format("public {0} {1}",
                  ei.EventHandlerType.Name,
                  ei.Name);
    
                Normal n = new Normal();
                n.Inlines.Add(new C1Run() { Text = text });
                doc.Blocks.Add(n);
            }
        }
    }
    

    If you run the project now, it will resemble the image below:

     

     The resulting document can be viewed and edited in the C1RichTextBox like any other. It can also be exported to HTML using the C1RichTextBox.Html property in the C1RichTextBox, or copied through the clipboard to applications such as Microsoft Word or Excel.

    You could use the same technique to create reports based on data from a database. In addition to formatted text, the C1Document object model supports the following features:

    Lists are created by adding C1List objects to the document. The C1List object has a C1List.ListItems property that contains C1ListItem objects, which are also blocks.

    Hyperlinks are created by adding C1Hyperlink objects to the document. The C1Hyperlink object has an C1Span.Inlines property that contains a collection of runs (typically C1Run elements that contain text), and a NavigateUri property that determines the action to be taken when the hyperlink is clicked.

    Images and other FrameworkElement objects are created by adding C1BlockUIContainer objects to the document. The C1BlockUIContainer object has a C1BlockUIContainer.Child property that can be set to any FrameworkElement object.

    Note that not all objects can be exported to HTML. Images are a special case that the HTML filter knows how to handle.