RadFixedDocumentEditor
RadFixedDocumentEditor allows you to create a RadFixedDocument in a flow-like manner and insert all desired elements one after another. When the document is rendered, the size of the elements will be automatically calculated. The editor provides a convenient API that enables the generation of documents, which automatically flow to pages.
Creating RadFixedDocumentEditor
Example 1 demonstrates how a RadFixedDocumentEditor instance can be created.
Example 1: Create RadFixedDocumentEditor
RadFixedDocument radFixedDocument = new RadFixedDocument();
RadFixedDocumentEditor radFixedDocumentEditor = new RadFixedDocumentEditor(radFixedDocument);
//Use RadFixedDocumentEditor...
radFixedDocumentEditor.Dispose();
RadFixedDocumentEditor inherits from IDisposable so it should be properly disposed when the document is created. Otherwise, some of the content may not be finished, i.e. it might not appear on the PDF document.
Sections
Section is a sequence of RadFixedPages with the same properties.
SectionProperties
The section properties are responsible for the page size, margins and orientation of RadFixedPages in a section. Below is the complete list of section properties:
PageSize: The size of the pages that are part of the section.
PageMargins: The page margins of a page.
-
PageRotation: The page rotation. This is enum that can take one of the following values:
- Rotate0: The page is not rotated. This is the default value.
- Rotate90: The page is rotated to 90°.
- Rotate180: The page is rotated to 180°.
- Rotate270: The page is rotated to 270°.
Example 2: Setting section properties
radFixedDocumentEditor.SectionProperties.PageSize = new Size(100,100);
radFixedDocumentEditor.SectionProperties.PageRotation = Telerik.Windows.Documents.Fixed.Model.Data.Rotation.Rotate90;
Starting New Section
The first section of a document starts as soon as a content is inserted to the editor. You can change the Section properties before inserting any content and they will be applied to the section that is automatically created.
Adding an additional section is achieved with the InsertSectionBreak() method as demonstrated in Example 2.
Example 3: Start a section
radFixedDocumentEditor.InsertSectionBreak();
If you want to change the properties of the next section, make sure to do it before you insert the section break. New properties are only used for newly created sections.
Starting New Page
All pages that have the same SectionProperties are part of the current section. To start a new page, you can use the following code:
Example 4: Start new page
radFixedDocumentEditor.InsertPageBreak();
Paragraphs
Paragraphs are blocks of flowing inlines - images and text.
ParagraphProperties
Similar to the section properties, paragraph has its own properties that are responsible for the way it looks.
SpacingBefore: Represents the spacing before.
SpacingAfter: Represents the spacing after.
LineSpacing: The spacing between the lines.
LineSpacingType: Specifies how to interpret the line spacing.
FirstLineIndent: The indent for the first line.
LeftIndent: The left indent.
RightIndent: The right indent.
BackgroundColor: The background color.
HorizontalAlignment: The horizontal alignment of the content.
ListId: The id of the list the paragraph belongs to. If null, then the paragraph belongs to no list.
ListLevel: The list level the paragraph belongs to.
Example 5: Setting paragraph properties
radFixedDocumentEditor.ParagraphProperties.SpacingAfter = 10;
radFixedDocumentEditor.ParagraphProperties.LineSpacingType = HeightType.Auto;
adFixedDocumentEditor.ParagraphProperties.BackgroundColor = new RgbColor(0, 100, 0);
radFixedDocumentEditor.ParagraphProperties.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
Starting New Paragraph
The first paragraph is created as soon as content is inserted in the editor. You can change paragraph properties before inserting content and when the first paragraph is created automatically, it will use the desired properties.
In order to start a new paragraph, use the code in Example 4.
Example 6: Start a paragraph
radFixedDocumentEditor.InsertParagraph();
The result of this method is that a new paragraph is started and it uses the current paragraph properties. Until a new paragraph is started, changes in the paragraph properties are not applied.
Inlines
A Paragraph is built of two types of inlines - runs and images.
Runs
Run represents a collection of characters that have the same properties.
CharacterProperties
The character properties that are responsible for the look of the runs are listed below.
FontSize: The font size.
Font: The font.
ForegroundColor: The foreground color.
HighlightColor: The highlight color.
-
BaselineAlignment: Describes how the baseline for a text-based element is positioned on the vertical axis, relative to the established baseline for text.
- Baseline: A baseline that is aligned at the actual baseline of the containing box.
- Subscript: A baseline that is aligned at the subscript position of the containing box.
- Superscript: A baseline that is aligned at the superscript position of the containing box.
-
UnderlinePattern: Тhe underline pattern. Two patterns are supported.
- None: There is no underline. This is the default value.
- Single: The underline is a single line.
UnderlineColor: The color of the underline.
-
StrikethroughPattern: Тhe strikethrough pattern. Two patterns are supported.
- None: There is no strikethrough. This is the default value.
- Single: The strikethrough is a single line.
StrikethroughColor: The color of the strikethrough.
Example 7: Setting CharacterProperties
radFixedDocumentEditor.CharacterProperties.FontSize = 12;
radFixedDocumentEditor.CharacterProperties.Font = FontsRepository.Courier;
radFixedDocumentEditor.CharacterProperties.HighlightColor = new RgbColor(10, 100, 80);
radFixedDocumentEditor.CharacterProperties.BaselineAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.BaselineAlignment.Subscript;
radFixedDocumentEditor.CharacterProperties.UnderlinePattern = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.UnderlinePattern.Single;
In order for the character properties to be respected, make sure to set them before inserting the Run.
Inserting a Run
There are a number of overloads that insert a run. The code snippet in Example 5 inserts new runs with specific font family, style and weight.
Example 8: Insert run
radFixedDocumentEditor.InsertRun("text");
radFixedDocumentEditor.InsertRun(new FontFamily("Helvetica"),"text");
There are a number of overloads that insert a run. The code snippet in Example 5 inserts a couple of new runs, one of which with a specific font family.
The '\r' and '\n' characters don't have the usual meaning of "go to next line" when they are inserted into a PDF document and you cannot simply insert text containing these characters to produce multiline text. Instead, you should split the text and insert a line break.
The code in Example 9 inserts a new run and a line break after it.
Example 9: Insert run and line break
radFixedDocumentEditor.InsertLine("Line of text");
Images
Image inline is a combination of an ImageSource object and its desired size.
Inserting Image
You can insert image inline using one of the following methods:
Example 10: Insert image
ImageSource imageSource = new ImageSource(new FileStream("image.jpeg", FileMode.Open));
radFixedDocumentEditor.InsertImageInline(imageSource);
radFixedDocumentEditor.InsertImageInline(imageSource, new Size(100, 100));
Tables
The Table class implements the IBlockElement interface and an instance of this class can be easily inserted as a new block in the document. You could insert the table using the InsertTable() method like illustrated in Example 8. RadFixedDocumentEditor takes care for positioning, measuring and splitting the table onto pages.
Example 11: Insert table
Table table = new Table();
TableRow firstRow = table.Rows.AddTableRow();
firstRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("cellText");
radFixedDocumentEditor.InsertTable(table);
For more detailed information on tables, check the Table documentation article.
Block Elements
The IBlockElement interface allows you to easily draw and split some block content onto pages. The interface is implemented by Block and Table classes. You can easily add some block element instance with RadFixedDocumentEditor using the InsertBlock() method like illustrated in Example 9.
Example 12: Insert Block element
Block block = new Block();
block.InsertText("Text");
radFixedDocumentEditor.InsertBlock(block);
Lists
You can easily insert list items with RadFixedDocumentEditor. The first thing you have to do is add a List in editor’s ListCollection by using the Lists property. Then, each time you want to add list item you have to set the appropriate ListId and ListLevel property values through RadFixedDocumentEditor’s ParagraphProperties. Inserting a new paragraph will result in adding a new list item.
The following code snippet shows how to add a new list to RadFixedDocumentEditor’s ListCollection and after that insert a paragraph with the corresponding list properties:
Example 13: Insert list
List list = radFixedDocumentEditor.Lists.AddList(ListTemplateType.NumberedDefault);
radFixedDocumentEditor.ParagraphProperties.ListId = list.Id;
radFixedDocumentEditor.ParagraphProperties.ListLevel = 0;
radFixedDocumentEditor.InsertParagraph();
Forms
With the RadFixedDocumentEditor class you can insert a Form (Form-XObject) element.
Example 14: Insert a form
radFixedDocumentEditor.InsertFormInline(formSource);
For more information on how to create a form, check the Form and FormSource articles.