Cellbi.DocFramework Quick Start
Sample code language:
Generate Documents Easily
It might be boring to read an article about new product starting from simple examples and move over to
more complex ones step by step. Most developers just prefer to start from code illustrating product features.
So we’ll do the same here, let’s go forward and write some code to solve a task of generating MS Word document,
we’ll see how this task can be made simple using Cellbi.DocFramework.
Let’s consider a task of generating document describing some product, such a document will contain product photo,
product features explained plus we’ll add company logo and add some document adorners.
Document sample generated by the Cellbi.DocFramework can be found here:
Sample
Click here to view screen shot of the final document:
Screen Shot
First of all we’ll need to create a new document add new document section and set page format to A4 page:
Document doc = new Document();
Section section = doc.AppendSection(String.Empty);
section.PageFormat = PageFormat.A4;
Document constructor creates empty document without any document nodes, but document structure will be completed to
include one section, one paragraph and one text block as soon as document is changed. The same happens with all document
tree nodes, they are auto completed on change.
Next step is to create company logo and add document title; here is the code to do this:
IParagraphList paragraphs = doc.GetParagraphs();
Paragraph paragraph = paragraphs[0];
int logoSize = PageUnits.FromCentimeters(1.5f);
paragraph.AppendPicture(Resources.Logo, logoSize, logoSize);
paragraph = doc.AppendParagraph(String.Empty);
string whiteSpace = new String(' ', 50);
paragraph.AppendText(whiteSpace);
IHasCharacterFormatting formatting = paragraph.AppendText("Gs Guitars");
formatting.Font.Name = CambriaFontName;
formatting.Font.Size = 24;
formatting.Font.Bold = true;
formatting.Font.Italic = true;
doc.AppendParagraph(String.Empty);
We get a list of document paragraphs, then we get first paragraph, and call AppendPicture to insert our
company logo from resource file. AppendText method called on the paragraph returns IHasCharacterFormating implementer,
which we use to format our title with 24 size Cambia bold and italic font.
Product photo can be added using AppendPicture method and specifying our image and picture size, below is the code showing this:
Bitmap guitar = Resources.Guitar;
Paragraph paragraph = doc.AppendParagraph(String.Empty);
int width = PageUnits.FromCentimeters(12.15f);
int height = PageUnits.FromCentimeters(4.0f);
paragraph.AppendPicture(guitar, width, height);
paragraph.TextAlignment = TextAlignment.Centered;
Adding product title is similar to adding document title, we will just use different formatting properties:
Paragraph paragraph = doc.AppendParagraph(text);
paragraph.Font.Name = CambriaFontName;
paragraph.Font.Size = 16;
paragraph.Font.Bold = true;
paragraph.Font.Italic = true;
paragraph.Font.Color = TitleColor;
paragraph.TextAlignment = TextAlignment.Centered;
Next we’ll add product specification. We’ll use bulleted list to create single level lists of features.
Below is the code illustrating how to add lists to a document:
List list = doc.AppendList();
IListLevelList levels = list.AppendLevels(items);
list.Font.Name = CalibriFontName;
list.Font.Size = 11;
From the code above it is clear how text formatting can be specified for text blocks and paragraphs, although it
is pretty easy, but there is another way to do the same task. We can create and use document styles for defining
text formatting properties of individual document parts. Document styles provide flexible way to format document text,
and it is easy to completely change document formatting changing the styles. E.g. here is the code to create several
styles used within the document we generated:
Style DocumentTitleStyle;
ParagraphStyle ProductTitleStyle;
DocumentTitleStyle = doc.AddStyle(StyleType.Character, "Document Title");
DocumentTitleStyle.Font.Name = CambriaFontName;
DocumentTitleStyle.Font.Size = 24;
DocumentTitleStyle.Font.Bold = true;
DocumentTitleStyle.Font.Italic = true;
ProductTitleStyle = doc.AddStyle(StyleType.Paragraph, "Product Title") as ParagraphStyle;
ProductTitleStyle.Font.Name = CambriaFontName;
ProductTitleStyle.Font.Size = 16;
ProductTitleStyle.Font.Bold = true;
ProductTitleStyle.Font.Italic = true;
ProductTitleStyle.Font.Color = TitleColor;
ProductTitleStyle.TextAlignment = TextAlignment.Centered;
Later in our document we can apply these styles to document text blocks and paragraphs. Here is an example of how
to apply a style to newly added text:
IHasCharacterFormatting formatting = paragraph.AppendText("Gs Guitars");
formatting.ApplyStyle(DocumentTitleStyle);
Apply style for newly added paragraph:
Paragraph paragraph = doc.AppendParagraph(text);
paragraph.Style = ProductTitleStyle;
The examples above can be found inside the sample code installed by the product installer.