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 is automatically calculated. The editor provides a convenient API that allows the generation of documents, which automatically flow to pages.
Creating RadFixedDocumentEditor
Example 1 demonstrates how to create a RadFixedDocumentEditor instance.
Example 1: Create RadFixedDocumentEditor
RadFixedDocument radFixedDocument = new RadFixedDocument();
RadFixedDocumentEditor radFixedDocumentEditor = new RadFixedDocumentEditor(radFixedDocument);
//Use RadFixedDocumentEditor...
radFixedDocumentEditor.Dispose();
RadFixedDocumentEditorinherits fromIDisposableso you must properly dispose it when the document is created. Otherwise, some of the content may not be finished, that is, it might not appear on the PDF document.
Sections
A Section is a sequence of RadFixedPages with the same properties.
SectionProperties
The section properties control the page size, margins, and orientation of RadFixedPages in a section. The following 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 an 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 content is inserted to the editor. You can change the section properties before inserting any content and they are applied to the section that is automatically created.
Add an additional section with the InsertSectionBreak() method as demonstrated in Example 2.
Example 3: Start a section
radFixedDocumentEditor.InsertSectionBreak();
To change the properties of the next section, ensure you 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, 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, a paragraph has its own properties that control its appearance.
-
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;
radFixedDocumentEditor.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 uses the desired properties.
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 starts and uses the current paragraph properties. Until a new paragraph starts, changes in the paragraph properties are not applied.
Inlines
A paragraph is built of two types of inlines—runs and images.
Runs
A Run represents a collection of characters that have the same properties.
CharacterProperties
The following character properties control the appearance of runs:
-
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: The 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: The 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;
For the character properties to take effect, set them before inserting the run.
In .NET Standard/.NET (Target OS: None) environments, fonts beyond the 14 standard ones require a FontsProvider implementation to be resolved correctly.
Inserting a Run
Several overloads insert a run. The code snippet in Example 8 inserts new runs with specific font family, style, and weight.
Example 8: Insert run
radFixedDocumentEditor.InsertRun("text");
radFixedDocumentEditor.InsertRun(new FontFamily("Helvetica"), "text");
Several overloads insert a run. The code snippet in Example 8 inserts a couple of new runs, one of which with a specific font family.
The '\r' and '\n' characters do not have the usual meaning of "go to next line" when inserted into a PDF document, and you cannot insert text containing these characters to produce multiline text. Instead, 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
An image inline is a combination of an ImageSource object and its desired size.
Inserting Image
You can insert an 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 you can insert an instance of this class as a new block in the document. Insert the table using the InsertTable() method as illustrated in Example 11. RadFixedDocumentEditor takes care of 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, see the Table documentation article.
Block Elements
The IBlockElement interface allows you to draw and split block content onto pages. The Block and Table classes implement this interface. Add a block element instance with RadFixedDocumentEditor using the InsertBlock() method as illustrated in Example 12.
Example 12: Insert Block element
Block block = new Block();
block.InsertText("Text");
radFixedDocumentEditor.InsertBlock(block);
Lists
You can insert list items with RadFixedDocumentEditor. First, add a List to the editor ListCollection by using the Lists property. Then, each time you want to add a list item, set the appropriate ListId and ListLevel property values through the RadFixedDocumentEditor ParagraphProperties. Inserting a new paragraph results in adding a new list item.
The following code snippet shows how to add a new list to the RadFixedDocumentEditor ListCollection and then 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();
More detailed information about lists is available in the List documentation article.
Forms
With the RadFixedDocumentEditor class you can insert a Form (Form-XObject) element.
Example 14: Insert a form
radFixedDocumentEditor.InsertFormInline(formSource);
There is an additional overload of InsertFormInline() that allows you to pass the size that should be used for the form.
For more information on how to create a form, see the Form and FormSource articles.