Creating Documents and Reports

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.Silverlight and C1.Silverlight.RichTextBox assemblies. Then edit the page constructor as follows:

      Visual Basic

      C#

The code creates the C1RichTextBox and assigns its 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:

      Visual Basic

      C#

The method starts by creating a new C1Document object and setting its 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:

      Visual Basic

      C#

The Heading paragraph extends Normal and makes the text bold:

      Visual Basic

      C#

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

      Visual Basic

      C#

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:

      Visual Basic

      C#

 

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:

      Visual Basic

      C#

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:

      Visual Basic

      C#

If you run the project now, you will see a window like the one shown below:

 

 

The resulting document can be viewed and edited in the C1RichTextBox like any other. It can also be exported to HTML using the 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
Lists are created by adding C1List objects to the document. The C1List object has a ListItems property that contains C1ListItem objects, which are also blocks.

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

      Images
Images and other FrameworkElement objects are created by adding C1BlockUIContainer objects to the document. The C1BlockUIContainer object has a 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.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.