# FixedContentEditor

`FixedContentEditor` simplifies creating and editing content on a PDF page or another `IContentRootElement`, such as a [RadFixedPage](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixedpage).

> note Unlike [RadFixedDocumentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/radfixeddocumenteditor), which arranges content automatically, `FixedContentEditor` requires you to set the `Position` of each element. Ensure that the page has enough remaining space for the element.

For a complete example, see the [knowledge base article on creating a PDF document with a logo and text](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/create-pdf-document-with-logo-and-text-using-fixedcontenteditor).

## Public API

The public application programming interface (API) includes the following methods and properties:

| Method/Property       | Description                                                                                                                                                                |
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Position`            | Gets or sets the current position of the editor.                                                                                                                           |
| `Clipping`            | Gets the current clipping region.                                                                                                                                          |
| `PushClipping`        | Adds a clipping region and returns an `IDisposable` that removes it when disposed.                                                                                         |
| `PopClipping`         | Removes and returns the last clipping region.                                                                                                                              |
| `DrawText`            | Draws text as a [TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment) at the current position. |
| `DrawImage`           | Draws an [Image](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/image) at the current position.                      |
| `DrawForm`            | Draws a [Form](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/form) at the current position.                         |
| `DrawWidget`          | Creates and draws a [Widget](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/widgets).                    |
| `DrawStampAnnotation` | Creates and draws a [StampAnnotation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/stamp).             |
| `DrawTextAnnotation`  | Creates and draws a [TextAnnotation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/text).               |
| `DrawLineAnnotation`  | Creates and draws a [LineAnnotation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/line).               |
| `DrawLine`            | Draws a line between two points.                                                                                                                                           |
| `DrawRectangle`       | Draws a rectangle [geometry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/geometry).                            |
| `DrawEllipse`         | Draws an ellipse [geometry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/geometry).                             |
| `DrawCircle`          | Draws a circle [geometry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/geometry).                               |
| `DrawPath`            | Draws a path [geometry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/geometry).                                 |
| `DrawTable`           | Draws a [Table](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/table/overview).                                    |
| `DrawBlock`           | Draws a [Block](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/block).                                             |
| `Draw`                | Draws the specified element.                                                                                                                                               |

## Creating FixedContentEditor with a Specified Position

`FixedContentEditor` is always associated with a single [RadFixedPage](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixedpage) (also known as `IContentRootElement`) which it takes as a constructor parameter when created. The following snippet shows how to create an editor.

### Example 1: Create a FixedContentEditor for a RadFixedPage

```C#
RadFixedDocument document = new RadFixedDocument();
var firstPage = document.Pages.AddPage();
FixedContentEditor fixedContentEditor = new FixedContentEditor(firstPage);
```

The editor maintains an internal [Position](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position) inside the content root element. When a new element is created, its position is set to the current position of the editor. You can specify the initial position of the editor when creating it.

The following snippet demonstrates how to create a `FixedContentEditor` with a specific initial [Position](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position).

### Example 2: Create a FixedContentEditor with an initial position

```C#
MatrixPosition matrixPosition = new MatrixPosition();
matrixPosition.Translate(20, 20); // Translates the position by (20, 20)
matrixPosition.Translate(30, 30); // Translates the position by (30, 30).
SimplePosition simplePosition = new SimplePosition();
simplePosition.Translate(20, 20); // Translates the position by (20, 20).
simplePosition.Translate(30, 30); // Translates the position by (30, 30) overwriting the previous translations.
FixedContentEditor matrixPositionFixedContentEditor = new FixedContentEditor(firstPage, matrixPosition);
FixedContentEditor simplePositionFixedContentEditor = new FixedContentEditor(firstPage, simplePosition);
```

## Inserting Elements

Composing a [RadFixedDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/radfixeddocument) normally requires creating all elements and specifying exactly how they look. The `FixedContentEditor` takes care of most things for you. This section explains how to add different types of elements.

### Inserting Text

Insert a [TextFragment](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/textfragment) with the `public void DrawText(string text)` method. The following snippet inserts a fragment with content "First text fragment.".

#### **Example 3: Insert a TextFragment with DrawText**

```C#
fixedContentEditor.DrawText("First text fragment.");
```

**Rendered TextFragment output**

![RadPdfProcessing FixedContentEditor output showing a drawn TextFragment](https://www.telerik.com/document-processing-libraries/documentation/assets/ac076a318bb43a8a3b511cabccbd98b4/radpdfprocessing_editing_fixedcontenteditor_01.png)

> note The `\r` and `\n` characters do not create line breaks when you insert text into a PDF document. Split multiline text and insert each line separately.

### Inserting Paragraph

The following snippet shows how to use the `Block` object to draw a paragraph.

#### **Example 4: Insert a paragraph with a Block**

```C#
Block block = new Block();
block.InsertText("First sentence.");
block.InsertText("Second sentence.");
fixedContentEditor.DrawBlock(block);
```

**Rendered paragraph output**

![RadPdfProcessing FixedContentEditor output showing a paragraph drawn from a Block](https://www.telerik.com/document-processing-libraries/documentation/assets/874a3586e0e935a9a4c8659a6b251782/radpdfprocessing_editing_fixedcontenteditor_02.png)

> tip Building a paragraph with the FixedContentEditor is much simpler than creating TextFragments yourself. The [Block](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/block) object flows the content of a paragraph for you if necessary.

### Inserting Image

`FixedContentEditor` provides several overloads for inserting an [Image](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/image):

* `DrawImage(Stream stream)`
* `DrawImage(Stream stream, double width, double height)`
* `DrawImage(Stream stream, Size size)`
* `DrawImage(ImageSource source)`
* `DrawImage(ImageSource source, Size size)`
* `DrawImage(ImageSource source, double width, double height)`

The following snippet shows how to add an image created from a Stream.

#### **Example 5: Insert an image from a stream**

```C#
using (Stream stream = File.OpenRead("Telerik_logo.jpg"))
{
	fixedContentEditor.DrawImage(stream, new Size(118, 28));
}
```

**Rendered image output**

![RadPdfProcessing FixedContentEditor output showing an inserted image from a stream](https://www.telerik.com/document-processing-libraries/documentation/assets/58f4dfd31d77c2a7055ea5e35679c100/radpdfprocessing_editing_fixedcontenteditor_04.png)

### Inserting Geometries

The following methods can be used to insert different [Geometries](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/geometry) in the document:

* `DrawLine(Point point1, Point point2)`: Inserts a line between the specified points.
* `DrawRectangle(Rect rectangle)`: Inserts a rectangle.
* `DrawEllipse(Point center, double radiusX, double radiusY)`: Inserts an ellipse.
* `DrawCircle(Point center, double radius)`: Inserts a circle.
* `DrawPath(PathGeometry pathGeometry)`: Inserts a custom path geometry.

The following snippet shows how to add an ellipse using one of the `FixedContentEditor` methods.

#### **Example 6: Insert an ellipse with FixedContentEditor**

```C#
fixedContentEditor.DrawEllipse(new Point(250, 70), 136, 48);
```

### Inserting Clipping

`FixedContentEditor` exposes a `Clipping` property, which defines the [Clipping](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/clipping) to be used for the inserted content elements. The following methods can be used to push and pop clippings:

* `PushClipping(GeometryBase clip)`: Inserts a new clipping defined from the specified geometry.
* `PushClipping(Rect clip)`: Inserts a new clipping defined from the specified rectangle.
* `PopClipping()`: Removes the last clipping added to 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. The following snippet shows how to push a clipping.

#### **Example 7: Push a clipping region before drawing text**

```C#
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 (fixedContentEditor.PushClipping(new Rect(0, 0, visisibleTextSize.Width, visisibleTextSize.Height)))
{
    fixedContentEditor.DrawText(text);
}
```

**Rendered clipping output**

![RadPdfProcessing FixedContentEditor output showing text clipped by a rectangular region](https://www.telerik.com/document-processing-libraries/documentation/assets/44d079139ceef8aa941c470ef967f515/radpdfprocessing_editing_fixedcontenteditor_03.png)

### Inserting Table

`FixedContentEditor` exposes the `DrawTable()` method, which allows you to 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.

The following snippet generates a table and draws it in a fixed size.

#### **Example 8: Insert a table with a fixed size**

```C#
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);
fixedContentEditor.Position.Translate(10, 10);
fixedContentEditor.DrawTable(table, new Size(180, double.PositiveInfinity));
```

**Rendered table output**

![RadPdfProcessing FixedContentEditor output showing a fixed-size table](https://www.telerik.com/document-processing-libraries/documentation/assets/2f67a261ac63e3caf39195c32f660d2c/radpdfprocessing_editing_fixedcontenteditor_06.png)

More detailed information about tables is available in the [Table](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/table/overview) documentation article.

### Inserting Forms

With the `FixedContentEditor` class you can insert a Form external object (Form-XObject) element.

#### **Example 9: Insert a form XObject with FixedContentEditor**

```C#
fixedContentEditor.DrawForm(formSource);
```

There are two more overloads of `DrawForm()` that allow you to pass the size that should be used for the form.

> note For more information about creating forms, see the [Form](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/form) and [FormSource](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/formsource/overview) articles.

### Inserting Widgets

The Widget annotations allow you to visualize the content of a FormField. With the API of `FixedContentEditor`, you can create and insert widgets to the PDF document. The `DrawWidget()` method has two overloads:

* **DrawWidget\<T>(FormField\<T> parentField, Size annotationSize)**: Creates a new [Widget](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/widgets) for the [FormField](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/interactive-forms/form-fields/formfields) parameter. It draws the widget with the specified annotation size. The root of the `FixedContentEditor` must support annotations.

  #### **Example 10: Insert a PushButtonField widget with DrawWidget**

  ```C#
  PushButtonField pushButton = new PushButtonField("button");

  document.AcroForm.FormFields.Add(pushButton);

  fixedContentEditor.Position.Translate(20, 450);
  fixedContentEditor.DrawWidget(pushButton, new Size(100, 20));
  ```

* **DrawWidget(RadioButtonField parentField, RadioOption option, Size annotationSize)**: Creates a new [RadioButtonWidget](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/annotations/widgets#radiobuttonwidget-class) and draws it with the specified annotation size. The root of the `FixedContentEditor` must support annotations. The second parameter represents the option that the widget visualizes.

  #### **Example 11: Insert RadioButtonField widgets with DrawWidget**

  ```C#
  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);

  fixedContentEditor.Position.Translate(20, 410);
  fixedContentEditor.DrawWidget(radio, radio.Options[0], new Size(20, 20));
  fixedContentEditor.Position.Translate(50, 410);
  fixedContentEditor.DrawWidget(radio, radio.Options[1], new Size(20, 20));
  fixedContentEditor.Position.Translate(80, 410);
  fixedContentEditor.DrawWidget(radio, radio.Options[2], new Size(20, 20));
  ```

## Positioning

The [Position](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/position) property exposed by `FixedContentEditor` provides an easy way to manipulate the position of inserted content elements.

The following snippet shows how to manipulate the position of the inserted content elements.

### Example 12: Scale and rotate inserted content

```C#
fixedContentEditor.Position.Scale(1.5, 0.5);
fixedContentEditor.Position.Rotate(10);
fixedContentEditor.DrawText("Image:");
fixedContentEditor.Position.Translate(0, 20);
using (Stream stream = File.OpenRead("Telerik_logo.jpg"))
{
	fixedContentEditor.DrawImage(stream, new Size(118, 28));
}
```

**Rendered positioning output**

![RadPdfProcessing FixedContentEditor output showing scaled and rotated content](https://www.telerik.com/document-processing-libraries/documentation/assets/52ce08e6f3692fd70ea4f5276de194e7/radpdfprocessing_editing_fixedcontenteditor_05.png)

## Changing Current Styles

`FixedContentEditor` has properties and methods that affect how it is rendered:

* `TextProperties` and `GraphicProperties`: Responsible for the properties of text and graphics. For more information, see the [Text and Graphic Properties](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/editing/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 GraphicProperties.

* `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.

## See Also

* [How to Generate a PDF Document from Images with FixedContentEditor](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/pdf-from-images-with-fixedcontenteditor)
* [Adding a Watermark to PDF Files Using RadPdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/add-watermark-pdf-radpdfprocessing)
* [Adding Images with a Shadow in PDF Documents](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/add-shadow-image-radpdfprocessing)
* [Splitting a Large Image Across Multiple PDF Pages](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/split-export-large-image-multiple-pdf-pages-radpdfprocessing)
* [Resizing Large Images to Fit in the PDF Page](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/resize-images-radpdfprocessing)
* [Inserting HTML Content into PDF TableCell with RadPdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/insert-html-content-into-pdf-tablecell-radpdfprocessing)
* [Drawing Rectangles with Text and Image Content with RadPdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/pdfprocessing-draw-rectangle-with-content)
* [Positioning Centered and Right-Aligned Text on the Same Line in PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/aligning-centered-right-margin-text-pdf-telerik-document-processing)
