RadFixedDocumentEditor
RadFixedDocumentEditor of PdfProcessing lets you create a RadFixedDocument in a flow-like way and insert elements one after another. During layout, the editor calculates element size automatically and moves content to new pages when needed.
Creating RadFixedDocumentEditor
Example 1 demonstrates how to create a RadFixedDocumentEditor instance.
Example 1: Create a RadFixedDocumentEditor
RadFixedDocument radFixedDocument = new RadFixedDocument();
RadFixedDocumentEditor radFixedDocumentEditor = new RadFixedDocumentEditor(radFixedDocument);
//Use RadFixedDocumentEditor...
radFixedDocumentEditor.Dispose();
RadFixedDocumentEditorimplementsIDisposable, so you must dispose it after you finish building the document. Otherwise, some content may not be finalized and may not appear in the exported PDF file.
Sections
A Section is a sequence of RadFixedPage instances that share the same properties.
SectionProperties
The section properties control the page size, margins, and orientation of the pages in a section. The following list includes the available properties:
-
PageSize: The size of the pages in the section. -
PageMargins: The page margins. -
PageRotation: The page rotation. This enum supports 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: Set section properties
radFixedDocumentEditor.SectionProperties.PageSize = new Size(100, 100);
radFixedDocumentEditor.SectionProperties.PageRotation = Telerik.Documents.Fixed.Model.Data.Rotation.Rotate90;
Starting New Section
The first section starts as soon as you insert content in the editor. If you set the section properties before you insert any content, the automatically created first section uses these values.
Add another section with the InsertSectionBreak() method, as shown in Example 2.
Example 3: Start a section
radFixedDocumentEditor.InsertSectionBreak();
To change the properties of the next section, set them before you insert the section break. New values apply only to 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 a new page
radFixedDocumentEditor.InsertPageBreak();
Paragraphs
Paragraphs contain flowing inline content such as text and images.
ParagraphProperties
Like sections, paragraphs have their own properties that control appearance.
-
SpacingBefore: The spacing before the paragraph. -
SpacingAfter: The spacing after the paragraph. -
LineSpacing: The spacing between lines. Defaults to1.15. WhenLineSpacingTypeisAuto, this acts as a line height multiplier. WhenLineSpacingTypeisExactorAtLeast, this specifies the line height in Device Independent Pixels (DIPs). -
LineSpacingType: Specifies how to interpret the line spacing using theHeightTypeenumeration (Auto,Exact,AtLeast). Defaults toAuto. -
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 that the paragraph belongs to. If the value isnull, the paragraph does not belong to a list. -
ListLevel: The list level the paragraph belongs to.
Example 5: Set paragraph properties
radFixedDocumentEditor.ParagraphProperties.SpacingAfter = 10;
radFixedDocumentEditor.ParagraphProperties.LineSpacingType = HeightType.Auto;
radFixedDocumentEditor.ParagraphProperties.BackgroundColor = new RgbColor(0, 100, 0);
radFixedDocumentEditor.ParagraphProperties.HorizontalAlignment = Telerik.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
Starting New Paragraph
The first paragraph starts as soon as you insert content in the editor. If you set paragraph properties before you insert content, the automatically created first paragraph uses these values.
To start a new paragraph, use the code in Example 4.
Example 6: Start a paragraph
radFixedDocumentEditor.InsertParagraph();
This method starts a new paragraph and uses the current paragraph properties. Property changes do not apply until a new paragraph starts.
Inlines
A paragraph consists of two inline types: runs and images.
Runs
A Run represents a sequence of characters that share the same properties.
Example 7: Set character properties
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.
Setting CharacterProperties
radFixedDocumentEditor.CharacterProperties.FontSize = 12;
radFixedDocumentEditor.CharacterProperties.Font = FontsRepository.Courier;
radFixedDocumentEditor.CharacterProperties.HighlightColor = new RgbColor(10, 100, 80);
radFixedDocumentEditor.CharacterProperties.BaselineAlignment = Telerik.Documents.Fixed.Model.Editing.Flow.BaselineAlignment.Subscript;
radFixedDocumentEditor.CharacterProperties.UnderlinePattern = Telerik.Documents.Fixed.Model.Editing.Flow.UnderlinePattern.Single;
For the character properties to take effect, set them before you insert 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 runs with a specific font family, style, and weight.
Example 8: Insert a run
radFixedDocumentEditor.InsertRun("text");
radFixedDocumentEditor.InsertRun(new FontFamily("Helvetica"), "text");
The
\rand\ncharacters do not create a new line when you insert text in a PDF document. To create multiline text, split the text and insert a line break explicitly.
The code in Example 9 inserts a new run and a line break after it.
Example 9: Insert a run and a line break
radFixedDocumentEditor.InsertLine("Line of text");
Images
An image inline is a combination of an ImageSource object and its desired size.
Inserting an Image
You can insert an image inline using one of the following methods:
Example 10: Insert an 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, so you can insert a table as a new block in the document. Insert it with the InsertTable() method, as shown in Example 11. RadFixedDocumentEditor handles positioning, measuring, and splitting the table across pages.
Example 11: Insert a 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 lets you draw and split block content across pages. The Block and Table classes implement this interface. Add a block element instance with RadFixedDocumentEditor by using the InsertBlock() method, as shown in Example 12.
Example 12: Insert a 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 through the Lists property. Then, each time you want to add a list item, set the ListId and ListLevel values through RadFixedDocumentEditor.ParagraphProperties. Each new paragraph becomes 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 a 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 article.
Forms
With RadFixedDocumentEditor, you can insert a form (Form XObject) element.
Example 14: Insert a form
radFixedDocumentEditor.InsertFormInline(formSource);
There is an additional overload of InsertFormInline() that lets you pass the size to use for the form.
For more information about creating forms, see the Form and FormSource articles.