The Block class is intended to arrange the elements added to it in a flow-like manner. It can be used for measuring, drawing, and splitting of FixedContentElements.
The most common usage of Block is to draw flowing content. Similarly to FixedContentEditor, a block can contain images, graphics or text. The same Block instance can only be drawn once.
Inserting TextFragments is achieved with one of the overloads of the Insert() method. Example 1 shows a simple insert by passing a string to the method.
Example 1: Insert text
Block block = new Block(); block.InsertText("Text");
Example 2 demonstrates how to insert text with a specific font family.
Example 2: Insert text with Arial font family
block.InsertText(new FontFamily("Arial"), "Text");
Example 3: Insert styled text
block.InsertText(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Bold, "Text");
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 insert a line break.
Inserting a line break results in the next element starting on a new line. The action is achieved with the InsertLineBreak() method as shown in Example 4.
Example 4: Break the line
Block provides the following methods for inserting images:
- block.InsertImage(imageSource, size);
- block.InsertImage(stream, size);
- block.InsertImage(imageSource, width, height);
- block.InsertImage(stream, width, height);
Geometries allow you to describe 2D shapes in a document. The following methods can be used to insert different geometries.
- block.InsertCircle(center, radius);
- block.InsertEllipse(center, radiusX, radiusY);
- block.InsertLine(point1, point2);
The Form (or also known as Form-XObject) is an object that can contain PDF content and can be sheared among the document. The Block class exposes the InsertForm() method that allows you insert a FormSource object in the document.
Example 5: Insert a form
The Block class has some properties and methods that affect how it will be rendered:
SpacingBefore: Represent 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.
VerticalAlignment: The vertical alignment of the content.
Bullet: The element that should be rendered as Block’s list bullet.
IndentAfterBullet: The indent size after the bullet element.
TextProperties and GraphicProperties: Responsible for text and graphic properties. For more information see the Text and Graphic Properties article.
SaveTextProperties(): Saves the TextProperties. It returns an IDisposable object which when disposed calls RestoreTextProperties() and can be used in using statement.
RestoreTextProperties(): Restores the TextProperties to their previous state.
SaveGraphicProperties(): Saves the GraphicProperties. It returns an IDisposable object which when disposed calls RestoreTextProperties() and can be used in using statement.
RestoreGraphicProperties(): Restores the GrahpicPropertie to their previous state.
SaveProperties(): Saves both text and graphic properties. It returns an IDisposable object which when disposed calls RestoreTextProperties() and can be used in using statement.
RestoreProperties(): Restores both text and graphic properties.
SetBullet(List list, int listLevel): This method helps you to easily set bullet related properties respecting the numbering and the formatting of some List class instance. More information about lists you may find in this article.
Clear(): Clears all elements in the block.
A Block can be drawn to the content using the Draw() method. The method accepts as a parameter a Rectangle, specifying the desired size and position relatively to the editor of the element.
Example 5: Draw block
Rect boundingRect = new Rect(new Point(0, 0), new Size(200, 300)); block.Draw(fixedContentEditor, boundingRect);
Every block can be drawn only once. Otherwise, an exception will be thrown.
Measuring a Block can be achieved with one of the overloads of the Measure() method. Invoking the method without a parameter will return the desired size of the elements in the block and set the block's DesiredSize property. The method is handy when you want to determine the size of the Block.
Calling the overload accepting available size measures the block in that specific size. Additionally to setting the DesiredSize property, it sets the PendingElements property with a collection of the elements that could not fit in the available size.
Example 7 creates a Block with the text "Hello RadPdfProcessing!" and measures it.
Example 7: Measure block
Block block = new Block(); block.InsertText("Hello RadPdfProcessing!"); Size size = block.Measure();
The Split() method of a Block returns a new Block with the same properties. The resulting block contains all pending elements that do not fit in the current block, based on the result of the last measure call.
The code in Example 8 splits a block in two. The first will contains text "Hello" and the second – "RadPdfProcessing!".
Example 8: Split block
Block helloBlock = new Block(); helloBlock.InsertText("Hello"); Size helloSize = helloBlock.Measure(); Block block = new Block(); block.InsertText("Hello RadPdfProcessing!"); Size size = block.Measure(helloSize); Block secondBlock = block.Split();