FixedContentEditor
FixedContentEditor is utility class that is intended to simplify the process of creating and editing the content of an IContentRootElement such as RadFixedPage.
Creating FixedContentEditor
FixedContentEditor is always associated to a single IContentRootElement which it takes as a constructor parameter when it is created. Example 1 shows how you can create an editor.
Example 1: Create FixedContentEditor
FixedContentEditor editor = new FixedContentEditor(contentRootElement);
The editor maintains an internal Position inside the content root element. When a new element is created, its position is being set to the current position of the editor. The initial position of the editor can be specified when it is created.
Example 2 demonstrates how you can create а FixedContentEditor with a specific initial position.
Example 2: Create FixedContentEditor with specific position
FixedContentEditor editor = new FixedContentEditor(contentRootElement, initialPosition);
Inserting Elements
Composing a RadFixedDocument normally requires to create all elements and specify exactly how they should look. The FixedContentEditor takes care of most things for you. This section explains how you can add different type of elements.
Inserting Text
Inserting а TextFragment can be done with the public void DrawText(string text) method. Example 3 inserts a fragment with content "First text fragment.".
Example 3: Insert TextFragment
editor.DrawText("First text fragment.");
Figure 1 shows the result of Example 3.
Figure 1: TextFragment result
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 it line by line.
Inserting Paragraph
Example 4 shows how you can use the Block object to draw a paragraph.
Example 4: Insert paragraph
Block block = new Block();
block.InsertText("First sentence.");
block.InsertText("Second sentence.");
editor.DrawBlock(block);
Figure 2 shows the result of Example 4.
Figure 2: Paragraph
Building a paragraph with the FixedContentEditor is much simpler than creating TextFragments yourself. The Block object would flow the content of a paragraph for you if this is necessary.
Inserting Image
FixedContentEditor provides several overloads for inserting an Image.
- public void DrawImage(Stream stream);
- public void DrawImage(Stream stream, double width, double height);
- public void DrawImage(Stream stream, Size size);
- public void DrawImage(ImageSource source);
- public void DrawImage(ImageSource source, Size size);
- public void DrawImage(ImageSource source, double width, double height);
Example 5 shows how you can add an image created from a Stream.
Example 5: Insert image
using (Stream stream = this.GetResourceStream("Telerik_logo.jpg"))
{
editor.DrawImage(stream, new Size(118, 28));
}
Figure 3: Image result
Inserting Geometries
The following methods can be used to insert different Geometries in the document:
- public void DrawLine(Point point1, Point point2): Inserts a line between the specified points.
- public void DrawRectangle(Rect rectangle): Inserts a rectangle.
- public void DrawEllipse(Point center, double radiusX, double radiusY): Inserts an ellipse.
- public void DrawCircle(Point center, double radius): Inserts a circle.
- public void DrawPath(PathGeometry pathGeometry): Inserts a custom path geometry.
Example 6 shows how you can add an ellipse using one of FixedContentEditor's methods.
Example 6: Insert ellipse
editor.DrawEllipse(new Point(250, 70), 136, 48);
Inserting Clipping
FixedContentEditor exposes a Clipping property, which defines the Clipping to be used for the inserted content elements. The following methods can be used to push and pop clippings:
- public IDisposable PushClipping(GeometryBase clip): Inserts a new clipping defined from the specified geometry.
- public IDisposable PushClipping(Rect clip): Inserts a new clipping defined from the specified rectangle.
- public Clipping PopClipping(): Pops the last clipping, which was inserted with the editor.
When the returned IDisposable object from the PushClipping() method is disposed, the clipping is popped from the clippings in the editor.
When a new clipping is pushed, it is set as a clipping to the current clipping in the editor. Example 7 shows how a clipping can be pushed.
Example 7: Push clipping
string visibleText = "The last word in this text is";
string text = string.Format("{0} clipped.", visibleText); //The last word in this text is clipped.
Block block = new Block();
block.InsertText(visibleText);
Size visisibleTextSize = block.Measure();
using (editor.PushClipping(new Rect(new Point(0, 0), visisibleTextSize)))
{
editor.DrawText(text);
}
Figure 4 shows the result of Example 7.
Figure 4: Clipping result
Inserting Table
FixedContentEditor exposes DrawTable() method, which allows you to easily position and draw tabular data in the PDF document. You can specify the size you need to fit the table in by using the appropriate overload of the DrawTable() method.
Example 8 generates a table and draws it in some fixed size.
Example 8: Insert table
Table table = new Table();
Border border = new Border();
table.DefaultCellProperties.Borders = new TableCellBorders(border, border, border, border);
table.DefaultCellProperties.Padding = new Thickness(10);
TableRow firstRow = table.Rows.AddTableRow();
firstRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("First cell");
firstRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("Second cell");
firstRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("Third cell");
TableRow secondRow = table.Rows.AddTableRow();
secondRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("Forth cell");
secondRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("Fifth cell");
secondRow.Cells.AddTableCell().Blocks.AddBlock().InsertText("Sixth cell");
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = document.Pages.AddPage();
FixedContentEditor editor = new FixedContentEditor(page);
editor.Position.Translate(10, 10);
editor.DrawTable(table, new Size(180, double.PositiveInfinity));
The table created in Example 8
More detailed information about tables is available in the Table documentation article.
Inserting Forms
With the FixedContentEditor class you can insert a Form (Form-XObject) element.
Example 9: Insert a form
editor.DrawForm(formSource);
For more information on how to create a form, check the Form and FormSource articles.
Inserting Widgets
The Widget annotations allow you visualize the content of a FormField. With the API of FixedContentEditor, you can easily create and insert widgets to the PDF document. The DrawWidget() method has two overloads:
-
DrawWidget
(FormField : Creates new Widget representing the FormField instance passed as a parameter and draws the widget with the specified annotation size. This method will add widget only in cases when the root of the FixedContentEditor supports annotations.parentField, Size annotationSize) Example 10: Insert PushButtonField with PushButtonWidget using DrawWidget
PushButtonField pushButton = new PushButtonField("button"); document.AcroForm.FormFields.Add(pushButton); editor.Position.Translate(20, 450); editor.DrawWidget(pushButton, new Size(100, 20));
-
DrawWidget(RadioButtonField parentField, RadioOption option, Size annotationSize): Creates new RadioButtonWidget and draws the widget with the specified annotation size. This method will add widget only in cases when the root of the FixedContentEditor supports annotations. The second parameter represents the option that should be visualized by the widget.
Example 11: Insert RadioButtonField with RadioButtonWidget using DrawWidget
RadioButtonField radio = new RadioButtonField("radio"); radio.Options.Add(new RadioOption("first radio")); radio.Options.Add(new RadioOption("second radio")); radio.Options.Add(new RadioOption("third radio")); radio.Value = radio.Options[1]; document.AcroForm.FormFields.Add(radio); editor.Position.Translate(20, 410); editor.DrawWidget(radio, radio.Options[0], new Size(20, 20)); editor.Position.Translate(50, 410); editor.DrawWidget(radio, radio.Options[1], new Size(20, 20)); editor.Position.Translate(80, 410); editor.DrawWidget(radio, radio.Options[2], new Size(20, 20));
Positioning
The Position property exposed by FixedContentEditor provides an easy way to manipulate the position of inserted content elements.
The code in Example 12 shows how to manipulate the position of the inserted content elements and Figure 5 shows the result of the code.
Example 12: Scale and rotate content
editor.Position.Scale(1.5, 0.5);
editor.Position.Rotate(10);
editor.DrawText("Image:");
editor.Position.Translate(0, 20);
using (Stream stream = this.GetResourceStream("Telerik_logo.jpg"))
{
editor.DrawImage(stream, new Size(118, 28));
}
Figure 5: Positioning result
Changing Current Styles
FixedContentEditor has some properties and methods that affect how it will be rendered:
TextProperties and GraphicProperties: Responsible for the properties of text and graphics. For more information see the Text and Graphic Properties article.
SaveTextProperties(): Saves the TextProperties. It returns an IDisposable object which calls RestoreTextProperties() when disposed and can be used in a using statement.
RestoreTextProperties(): Restores the TextProperties.
SaveGraphicProperties(): Saves the GraphicProperties. It returns an IDisposable object which calls RestoreGraphicProperties() when disposed and can be used in a using statement.
RestoreGraphicProperties(): Restores the GrahpicProperties.
SaveProperties(): Saves both the text and the graphic properties. It returns an IDisposable object which calls RestoreProperties() when disposed and can be used in a using statement.
RestoreProperties(): Restores both text and graphic properties.