FixedContentEditor
FixedContentEditor is intended to simplify the process of creating and editing the content of a PDF page, also known as IContentRootElement or simply RadFixedPage.
Unlike RadFixedDocumentEditor which manages the document's content in a flow-like manner and allows you to insert all desired elements one after another without calculating the elements' position, the FixedContentEditor requires managing the Position at which the document elements will be drawn. This will give you the possibility to draw the respective element at a fixed position. However, you should be careful about the available remaining space on the page and the space needed for the element to be drawn. A complete example of how to create a PDF document from scratch is available in the How to Generate a PDF Document with Logo and Text using FixedContentEditor KB article.
Public API
Method/Property | Description |
---|---|
Position | Gets or sets the current position of the editor. |
Clipping | Defines the outline of other content elements like Image and Path. |
PushClipping | Inserts a new clipping defined from the specified object depending on the overload. |
PopClipping | Pops the last clipping, which was inserted with the editor. |
DrawText | Draws text as a TextFragment at the current position and with size depending on the overload. |
DrawImage | Draws an Image at the current position and with size depending on the overload. |
DrawForm | Draws a Form at the current position and with size depending on the overload. |
DrawWidget | Creates a Widget with different type and size depending on the overload. |
DrawStampAnnotation | Creates a new StampAnnotation and draws it with a specified size and name. |
DrawTextAnnotation | Creates a new TextAnnotation and draws it with a specified size and text. |
DrawLineAnnotation | Creates a new LineAnnotation with starting point the current point of the editor and end point the current point of the editor plus the given distances. |
DrawLine | Draws a line from point A to point B. |
DrawRectangle | Draws a rectangle (Geometry). |
DrawEllipse | Draws an ellipse (Geometry). |
DrawCircle | Draws a circle (Geometry). |
DrawPath | Draws a path (Geometry). |
DrawTable | Draws a Table with size depening on the overload. |
DrawBlock | Draws a Block with size depening on the overload. |
Draw | Draws the specified element. |
Creating FixedContentEditor with a Specified Position
FixedContentEditor is always associated with a single RadFixedPage (also known as 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
RadFixedDocument document = new RadFixedDocument();
var firstPage = document.Pages.AddPage();
FixedContentEditor fixedContentEditor = new FixedContentEditor(firstPage);
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 a FixedContentEditor with a specific initial Position.
Example 2: Create FixedContentEditor with a specific position
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 simplePositionfixedContentEditor = new FixedContentEditor(firstPage,matrixPosition);
FixedContentEditor matrixPositionfixedContentEditor = new FixedContentEditor(firstPage,matrixPosition);
Inserting Elements
Composing a RadFixedDocument normally requires creating all elements and specifying 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 a 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
fixedContentEditor.DrawText("First text fragment.");
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.");
fixedContentEditor.DrawBlock(block);
Figure 2: Paragraph
Building a paragraph with the FixedContentEditor is much simpler than creating TextFragments yourself. The Block object will 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"))
{
fixedContentEditor.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
fixedContentEditor.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)))
{
fixedContentEditor.DrawText(text);
}
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);
fixedContentEditor.Position.Translate(10, 10);
fixedContentEditor.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
fixedContentEditor.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); fixedContentEditor.Position.Translate(20, 450); fixedContentEditor.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); 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 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
fixedContentEditor.Position.Scale(1.5, 0.5);
fixedContentEditor.Position.Rotate(10);
fixedContentEditor.DrawText("Image:");
fixedContentEditor.Position.Translate(0, 20);
using (Stream stream = this.GetResourceStream("Telerik_logo.jpg"))
{
fixedContentEditor.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.
See Also
- RadFixedPage
- Block
- Position
- TextFragment
- Image
- Geometry
- Clipping
- Table
- How to Generate a PDF Document from Images with FixedContentEditor
- Adding a Watermark to PDF Files Using RadPdfProcessing
- Adding Images with a Shadow in PDF Documents
- Splitting a Large Image Across Multiple PDF Pages
- Resizing Large Images to Fit in the PDF Page