FloatingImage
FloatingImage is an inline-level anchor flow document element linked with a floating image object. The supported image extensions are JPG, JPEG, PNG, BMP, TIFF, TIF, GIF, ICON, SVG, WMF, and EMF.
Inserting a FloatingImage
Example 1 shows how to create a FloatingImage and add it to a Paragraph.
Example 1: Create a Floating Image and Insert It in a Paragraph
FloatingImage floatingImage = new FloatingImage(document);
paragraph.Inlines.Add(floatingImage);
The parent
Paragraphmust belong to the same document that is passed to the constructor of theFloatingImage.
You can add an image at a specific index in the Inlines collection of a paragraph using the Insert() method. The following example shows how to add a FloatingImage at the beginning of a paragraph:
Example 2: Add a Floating Image to a Specific Position
//Create an image
FloatingImage floatingImage = new FloatingImage(document);
//Assign a source to the image
string pathToImage = "path to image file";
string imageExtension = "png"; //or "jpeg", "bmp", etc.
floatingImage.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(File.ReadAllBytes(pathToImage), imageExtension);
//Insert the image at the desired position in a Paragraph
paragraph.Inlines.Insert(0, floatingImage);
You can also use the AddFloatingImage() method of the Inlines collection of a paragraph. The method creates a new FloatingImage, adds it to the paragraph, and returns it.
Example 3: Using the AddFloatingImage Method
FloatingImage floatingImage = paragraph.Inlines.AddFloatingImage();
You can also insert a FloatingImage element in a RadFlowDocument with RadFlowDocumentEditor.
Example 4: Insert a Floating Image Using RadFlowDocumentEditor
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
using (Stream stream = File.OpenRead("Telerik_logo.png"))
{
editor.InsertFloatingImage(stream, "png", new Size(118, 28));
}
Modifying a FloatingImage
The FloatingImage element exposes the following properties:
-
Image: Represents the image object that is contained in theFloatingImage. It is a read-only composite object that contains the properties listed below:ImageSource: Specifies the image source that is visualized in the image object.Name: Represents the name of the image.Width: The width of the image.Height: The height of the image.Size: The size of the image. Can also be set toSize.Empty.IsHorizontallyFlipped: Specifies whether the image is horizontally flipped.IsVerticallyFlipped: Specifies whether the image is vertically flipped.RotationAngle: Specifies the rotation angle of the image.LockAspectRatio: Determines whether the aspect ratio between the width and the height of the image is preserved.
-
AllowOverlap: Specifies whether the image is allowed to overlap the contents of the other image objects. The default value istrue. -
IsLocked: Specifies if the anchor can be modified at runtime. -
ShapeWrapping: Specifies the image wrapping. It is a composite object and contains the properties listed below:WrappingType: The wrapping type. The default value isSquare.TextWrap: The text wrap. The default value isBothSides.
-
LayoutInCell: Indicates if the layout of the image is calculated relative to the cell that holds the shape. -
IsBehindDocument: Indicates if the shape is displayed behind the document content. -
ZIndex: Specifies the ZIndex of the floating image. -
Margin: Represents the margins for the floating image. -
HorizontalPosition: Specifies the horizontal position of the floating image. It is a composite object that contains the properties listed below:ValueType: Specifies whether the positioning is based on theOffsetor theAlignmentvalue. The default value isOffset.RelativeFrom: Specifies the relative horizontal positioning base. The default value isColumn.Alignment: Specifies the alignment to be used if the position type isAlignment.Offset: Specifies the offset to be used if the position type isOffset.
-
VerticalPosition: Specifies the vertical position of the floating image. It is a composite object that contains the properties listed below:ValueType: Specifies whether the positioning is based on theOffsetor theAlignmentvalue. The default value isOffset.RelativeFrom: Specifies the relative vertical positioning base. The default value isColumn.Alignment: Specifies the alignment to be used if the position type isAlignment.Offset: Specifies the offset to be used if the position type isOffset.
Working with Image Size
This section explains the behavior of the Size property of the Image object in a FloatingImage and how the image sizes are exported to the supported formats.
-
Insert image without specifying its size
- Model: Size is
Size.Empty. - Export to DOCX: Size is decoded.
- Export to RTF: Size is decoded.
- Export to HTML: Width and height attributes are not exported.
- Export to PDF: Size is decoded.
- Model: Size is
-
Insert image and set its size to Size.Empty
- Model: Size is
Size.Empty. - Export to DOCX: Size is (1,1).
- Export to RTF: Size is (1,1).
- Export to HTML: Width and height attributes are not exported.
- Export to PDF: Size is (1,1).
- Model: Size is
-
Insert image without specifying its size and obtain its size later (through the property getter)
- Model: Size is decoded.
Exporting to PDF
WordsProcessing enables you to export documents with floating images to PDF. The set of supported properties is limited compared to other document formats. The following list contains the properties that the PdfFormatProvider class does not support:
IsHorizontallyFlippedIsVerticallyFlippedRotationAngleAllowOverlap: All images are drawn as ifAllowOverlapis set totrue.LayoutInCell: The image is always drawn inside the cell.- When exporting floating images that are anchored to elements inside tables, the exporter inserts them on a separate paragraph inside that cell, disregarding their wrapping properties.